From 333b322b0cd460ae049a278fb7869f0ae9162b78 Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Tue, 13 Oct 2020 07:52:47 +0800 Subject: [PATCH 1/8] fix error checking image --- builder/parser/docker_compose.go | 2 +- builder/sources/registry.go | 8 ++++---- builder/sources/registry/manifest.go | 26 +++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/builder/parser/docker_compose.go b/builder/parser/docker_compose.go index 6548c6dd5..5f513c15f 100644 --- a/builder/parser/docker_compose.go +++ b/builder/parser/docker_compose.go @@ -195,7 +195,7 @@ func (d *DockerComposeParse) Parse() ParseErrorList { d.logger.Debug(fmt.Sprintf("start check service %s ", service.name), map[string]string{"step": "service_check", "status": "running"}) exist, err := sources.ImageExist(service.image.String(), hubUser, hubPass) if err != nil { - logrus.Errorf("check image exist failure %s", err.Error()) + logrus.Errorf("check image(%s) exist failure %s", service.image.String(), err.Error()) } if !exist { d.errappend(ErrorAndSolve(FatalError, fmt.Sprintf("服务%s镜像%s检测失败", serviceName, service.image.String()), SolveAdvice("modify_compose", fmt.Sprintf("请确认%s服务镜像名称是否正确或镜像仓库访问是否正常", serviceName)))) diff --git a/builder/sources/registry.go b/builder/sources/registry.go index 6bc0424c4..9867c3048 100644 --- a/builder/sources/registry.go +++ b/builder/sources/registry.go @@ -19,8 +19,9 @@ package sources import ( + "fmt" + "github.com/goodrain/rainbond/builder/sources/registry" - "github.com/Sirupsen/logrus" "github.com/docker/distribution/reference" ) @@ -67,9 +68,8 @@ func ImageExist(imageName, user, password string) (bool, error) { } } tag := GetTagFromNamedRef(name) - _, err = reg.ManifestV2(reference.Path(name), tag) - if err != nil { - rerr = err + if err := reg.CheckManifest(reference.Path(name), tag); err != nil { + rerr = fmt.Errorf("[ImageExist] check manifest v2: %v", err) continue } return true, nil diff --git a/builder/sources/registry/manifest.go b/builder/sources/registry/manifest.go index ccd7fb587..495a2997b 100644 --- a/builder/sources/registry/manifest.go +++ b/builder/sources/registry/manifest.go @@ -20,14 +20,17 @@ package registry import ( "bytes" + "fmt" "io/ioutil" "net/http" + "github.com/Sirupsen/logrus" manifestV1 "github.com/docker/distribution/manifest/schema1" manifestV2 "github.com/docker/distribution/manifest/schema2" digest "github.com/opencontainers/go-digest" ) +// Manifest - func (registry *Registry) Manifest(repository, reference string) (*manifestV1.SignedManifest, error) { url := registry.url("/v2/%s/manifests/%s", repository, reference) registry.Logf("registry.manifest.get url=%s repository=%s reference=%s", url, repository, reference) @@ -58,9 +61,10 @@ func (registry *Registry) Manifest(repository, reference string) (*manifestV1.Si return signedManifest, nil } +// ManifestV2 - func (registry *Registry) ManifestV2(repository, reference string) (*manifestV2.DeserializedManifest, error) { url := registry.url("/v2/%s/manifests/%s", repository, reference) - registry.Logf("registry.manifest.get url=%s repository=%s reference=%s", url, repository, reference) + logrus.Debugf("registry.manifest.get url=%s repository=%s reference=%s", url, repository, reference) req, err := http.NewRequest("GET", url, nil) if err != nil { @@ -70,7 +74,7 @@ func (registry *Registry) ManifestV2(repository, reference string) (*manifestV2. req.Header.Set("Accept", manifestV2.MediaTypeManifest) resp, err := registry.Client.Do(req) if err != nil { - return nil, err + return nil, fmt.Errorf("do request: %v", err) } defer resp.Body.Close() @@ -82,11 +86,25 @@ func (registry *Registry) ManifestV2(repository, reference string) (*manifestV2. deserialized := &manifestV2.DeserializedManifest{} err = deserialized.UnmarshalJSON(body) if err != nil { - return nil, err + return nil, fmt.Errorf("unmarshal JSON: %v", err) } return deserialized, nil } +// CheckManifest checks if the manifest of the given image is exist. +func (registry *Registry) CheckManifest(repository, reference string) error { + url := registry.url("/v2/%s/manifests/%s", repository, reference) + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return err + } + + _, err = registry.Client.Do(req) + return err +} + +// ManifestDigest - func (registry *Registry) ManifestDigest(repository, reference string) (digest.Digest, error) { url := registry.url("/v2/%s/manifests/%s", repository, reference) registry.Logf("registry.manifest.head url=%s repository=%s reference=%s", url, repository, reference) @@ -101,6 +119,7 @@ func (registry *Registry) ManifestDigest(repository, reference string) (digest.D return digest.Parse(resp.Header.Get("Docker-Content-Digest")) } +// DeleteManifest - func (registry *Registry) DeleteManifest(repository string, digest digest.Digest) error { url := registry.url("/v2/%s/manifests/%s", repository, digest) registry.Logf("registry.manifest.delete url=%s repository=%s reference=%s", url, repository, digest) @@ -119,6 +138,7 @@ func (registry *Registry) DeleteManifest(repository string, digest digest.Digest return nil } +// PutManifest - func (registry *Registry) PutManifest(repository, reference string, signedManifest *manifestV1.SignedManifest) error { url := registry.url("/v2/%s/manifests/%s", repository, reference) registry.Logf("registry.manifest.put url=%s repository=%s reference=%s", url, repository, reference) From f108e4bc015bece6d8411d8b545a4faaea606cd6 Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Tue, 13 Oct 2020 18:06:50 +0800 Subject: [PATCH 2/8] bug fix: circular dependency --- cmd/init-probe/healthy/depends_health.go | 7 +------ worker/appm/conversion/version.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/init-probe/healthy/depends_health.go b/cmd/init-probe/healthy/depends_health.go index b57e4dc27..a200a1291 100644 --- a/cmd/init-probe/healthy/depends_health.go +++ b/cmd/init-probe/healthy/depends_health.go @@ -78,12 +78,7 @@ func NewDependServiceHealthController() (*DependServiceHealthController, error) } dsc.endpointClient = v2.NewEndpointDiscoveryServiceClient(cli) dsc.clusterClient = v2.NewClusterDiscoveryServiceClient(cli) - nameIDs := strings.Split(os.Getenv("DEPEND_SERVICE"), ",") - for _, nameID := range nameIDs { - if len(strings.Split(nameID, ":")) > 0 { - dsc.dependServiceNames = append(dsc.dependServiceNames, strings.Split(nameID, ":")[0]) - } - } + dsc.dependServiceNames = strings.Split(os.Getenv("STARTUP_SEQUENCE_DEPENDENCIES"), ",") return &dsc, nil } diff --git a/worker/appm/conversion/version.go b/worker/appm/conversion/version.go index 9e82c53ef..a99e2c74b 100644 --- a/worker/appm/conversion/version.go +++ b/worker/appm/conversion/version.go @@ -177,6 +177,10 @@ func createEnv(as *v1.AppService, dbmanager db.Manager) (*[]corev1.EnvVar, error Name: "LOGGER_DRIVER_NAME", Value: "streamlog", }) + + bootSeqDepServiceIDs := as.ExtensionSet["boot_seq_dep_service_ids"] + logrus.Infof("boot sequence dep service ids: %s", bootSeqDepServiceIDs) + //set relation app outer env relations, err := dbmanager.TenantServiceRelationDao().GetTenantServiceRelations(as.ServiceID) if err != nil { @@ -202,14 +206,20 @@ func createEnv(as *v1.AppService, dbmanager db.Manager) (*[]corev1.EnvVar, error return nil, err } var Depend string + var startupSequenceDependencies []string for _, sa := range serviceAliass { if Depend != "" { Depend += "," } Depend += fmt.Sprintf("%s:%s", sa.ServiceAlias, sa.ServiceID) + + if bootSeqDepServiceIDs != "" && strings.Contains(bootSeqDepServiceIDs, sa.ServiceID) { + startupSequenceDependencies = append(startupSequenceDependencies, sa.ServiceAlias) + } } envs = append(envs, corev1.EnvVar{Name: "DEPEND_SERVICE", Value: Depend}) envs = append(envs, corev1.EnvVar{Name: "DEPEND_SERVICE_COUNT", Value: strconv.Itoa(len(serviceAliass))}) + envs = append(envs, corev1.EnvVar{Name: "STARTUP_SEQUENCE_DEPENDENCIES", Value: strings.Join(startupSequenceDependencies, ",")}) sid2alias := make(map[string]string, len(serviceAliass)) for _, alias := range serviceAliass { From 979add64a5e7848fea001771047aefb5dfcd1ae5 Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Thu, 15 Oct 2020 18:08:28 +0800 Subject: [PATCH 3/8] bug fix: circular dependency --- api/handler/service_batch_operation.go | 20 ++++++++++++++-- api/handler/service_batch_operation_test.go | 26 +++++++++++++++++++++ api/handler/tenantsResource.go | 2 +- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/api/handler/service_batch_operation.go b/api/handler/service_batch_operation.go index d8121a2a9..e02832d83 100644 --- a/api/handler/service_batch_operation.go +++ b/api/handler/service_batch_operation.go @@ -312,18 +312,19 @@ func (s *ServiceDependency) buildLinkListByHead(l *list.List) []*list.List { if !ok { copy := list.New() copy.PushBackList(l) - return []*list.List{l} + return []*list.List{copy} } var result []*list.List for _, depsid := range depsids { // child node is already in the linked list - if alreadyInLinkedList(l, depsid) { + if alreadyInLinkedList(l, depsid) || s.childInLinkedList(l, depsid) { copy := list.New() copy.PushBackList(l) result = append(result, copy) continue } + newl := list.New() newl.PushBackList(l) newl.PushBack(depsid) @@ -341,6 +342,21 @@ func (s *ServiceDependency) buildLinkListByHead(l *list.List) []*list.List { return result } +func (s *ServiceDependency) childInLinkedList(l *list.List, sid string) bool { + depsids, ok := s.sid2depsids[sid] + if !ok { + return false + } + + for _, depsid := range depsids { + if alreadyInLinkedList(l, depsid) { + return true + } + } + + return false +} + func alreadyInLinkedList(l *list.List, depsid string) bool { pre := l.Back() for pre != nil { diff --git a/api/handler/service_batch_operation_test.go b/api/handler/service_batch_operation_test.go index 491cf0dcf..aa36711eb 100644 --- a/api/handler/service_batch_operation_test.go +++ b/api/handler/service_batch_operation_test.go @@ -167,6 +167,32 @@ func TestBuildLinkListByHead(t *testing.T) { return []*list.List{l} }(), }, + { + name: "spring cloud pig", + l: func() *list.List { + l := list.New() + l.PushBack("banana") + return l + }(), + sid2depsids: map[string][]string{ + "apple": []string{"banana"}, + "banana": []string{"apple", "cat", "dog"}, + }, + want: func() []*list.List { + l1 := list.New() + l1.PushBack("banana") + + l2 := list.New() + l2.PushBack("banana") + l2.PushBack("cat") + + l3 := list.New() + l3.PushBack("banana") + l3.PushBack("dog") + + return []*list.List{l1, l2, l3} + }(), + }, } for idx := range tests { diff --git a/api/handler/tenantsResource.go b/api/handler/tenantsResource.go index 9913ef33a..cc329a42c 100644 --- a/api/handler/tenantsResource.go +++ b/api/handler/tenantsResource.go @@ -22,7 +22,7 @@ func CheckTenantResource(tenant *dbmodel.Tenants, needMemory int) error { } clusterInfo, err := GetTenantManager().GetAllocatableResources() if err != nil { - logrus.Errorf("get cluster resources failure for check tenant resource.", err.Error()) + logrus.Errorf("get cluster resources failure for check tenant resource: %v", err.Error()) } if clusterInfo != nil { clusterAvailMemory := clusterInfo.AllMemory - clusterInfo.RequestMemory From e57898db1667266ba1600ee90d1f3093d9228499 Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Wed, 21 Oct 2020 10:31:40 +0800 Subject: [PATCH 4/8] proxy_buffer_size and proxy_buffer_numbers --- api/handler/gateway_action.go | 13 +++++++++++++ api/model/gateway_model.go | 2 ++ release.sh | 6 +----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/api/handler/gateway_action.go b/api/handler/gateway_action.go index 7ceee4b85..9a4a965e0 100644 --- a/api/handler/gateway_action.go +++ b/api/handler/gateway_action.go @@ -643,6 +643,16 @@ func (g *GatewayAction) RuleConfig(req *apimodel.RuleConfigReq) error { Key: "proxy-body-size", Value: strconv.Itoa(req.Body.ProxyBodySize), }) + configs = append(configs, &model.GwRuleConfig{ + RuleID: req.RuleID, + Key: "proxy-buffer-size", + Value: strconv.Itoa(req.Body.ProxyBufferSize) + "k", + }) + configs = append(configs, &model.GwRuleConfig{ + RuleID: req.RuleID, + Key: "proxy-buffer-numbers", + Value: strconv.Itoa(req.Body.ProxyBufferNumbers), + }) setheaders := make(map[string]string) for _, item := range req.Body.SetHeaders { if strings.TrimSpace(item.Key) == "" { @@ -661,10 +671,12 @@ func (g *GatewayAction) RuleConfig(req *apimodel.RuleConfigReq) error { Value: v, }) } + rule, err := g.dbmanager.HTTPRuleDao().GetHTTPRuleByID(req.RuleID) if err != nil { return err } + tx := db.GetManager().Begin() defer func() { if r := recover(); r != nil { @@ -686,6 +698,7 @@ func (g *GatewayAction) RuleConfig(req *apimodel.RuleConfigReq) error { tx.Rollback() return err } + if err := g.SendTask(map[string]interface{}{ "service_id": req.ServiceID, "action": "update-rule-config", diff --git a/api/model/gateway_model.go b/api/model/gateway_model.go index 0159f33ed..26fc5e533 100644 --- a/api/model/gateway_model.go +++ b/api/model/gateway_model.go @@ -129,6 +129,8 @@ type Body struct { ProxyBodySize int `json:"proxy_body_size,omitempty" validate:"proxy_body_size|required"` SetHeaders []*SetHeader `json:"set_headers,omitempty" ` Rewrites []*Rewrite `json:"rewrite,omitempty"` + ProxyBufferSize int `json:"proxy_buffer_size,omitempty"` + ProxyBufferNumbers int `json:"proxy_buffer_numbers,omitempty"` } //SetHeader set header diff --git a/release.sh b/release.sh index 2cb292cf3..c5be71076 100755 --- a/release.sh +++ b/release.sh @@ -4,11 +4,7 @@ set -o errexit # define package name WORK_DIR=/go/src/github.com/goodrain/rainbond BASE_NAME=rainbond -IMAGE_BASE_NAME=rainbond -if [ $BUILD_IMAGE_BASE_NAME ]; -then -IMAGE_BASE_NAME=${BUILD_IMAGE_BASE_NAME} -fi +IMAGE_BASE_NAME=${BUILD_IMAGE_BASE_NAME:-'rainbond'} GO_VERSION=1.13 GATEWAY_GO_VERSION=1.13-alpine From e7bacaa8c2df54531f1e4b594d14b1122425dad0 Mon Sep 17 00:00:00 2001 From: barnettZQG Date: Thu, 22 Oct 2020 10:23:03 +0800 Subject: [PATCH 5/8] change --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 10f1b5595..b42889df3 100644 --- a/.gitignore +++ b/.gitignore @@ -54,4 +54,5 @@ test.db *.bak temp -.scannerwork \ No newline at end of file +.scannerwork +.cache \ No newline at end of file From 42b5bd647450de39301c98e003b1d861816b9bbd Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Thu, 22 Oct 2020 10:47:43 +0800 Subject: [PATCH 6/8] ignore endpoints without ready addresses --- gateway/store/store.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gateway/store/store.go b/gateway/store/store.go index aa624c2b4..8481a32d7 100644 --- a/gateway/store/store.go +++ b/gateway/store/store.go @@ -690,7 +690,6 @@ func (s *k8sStore) ListVirtualService() (l7vs []*v1.VirtualService, l4vs []*v1.V // ingressIsValid checks if the specified ingress is valid func (s *k8sStore) ingressIsValid(ing *extensions.Ingress) bool { - var endpointKey string if ing.Spec.Backend != nil { // stream endpointKey = fmt.Sprintf("%s/%s", ing.Namespace, ing.Spec.Backend.ServiceName) @@ -724,7 +723,7 @@ func (s *k8sStore) ingressIsValid(ing *extensions.Ingress) bool { return false } for _, ep := range endpoint.Subsets { - if (ep.Addresses == nil || len(ep.Addresses) == 0) && (ep.NotReadyAddresses == nil || len(ep.NotReadyAddresses) == 0) { + if len(ep.Addresses) == 0 { logrus.Debugf("Endpoints(%s) is empty, ignore it", endpointKey) return false } From 577bd9be901a7f07b5270871a49340bdc2f76e3e Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Thu, 22 Oct 2020 11:13:12 +0800 Subject: [PATCH 7/8] check ready addresses --- gateway/store/store.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/gateway/store/store.go b/gateway/store/store.go index 8481a32d7..a9d6ebae6 100644 --- a/gateway/store/store.go +++ b/gateway/store/store.go @@ -718,20 +718,27 @@ func (s *k8sStore) ingressIsValid(ing *extensions.Ingress) bool { logrus.Errorf("Cant not convert %v to %v", reflect.TypeOf(item), reflect.TypeOf(endpoint)) return false } - if endpoint.Subsets == nil || len(endpoint.Subsets) == 0 { + if len(endpoint.Subsets) == 0 { logrus.Debugf("Endpoints(%s) is empty, ignore it", endpointKey) return false } - for _, ep := range endpoint.Subsets { - if len(ep.Addresses) == 0 { - logrus.Debugf("Endpoints(%s) is empty, ignore it", endpointKey) - return false - } + if !hasReadyAddresses(endpoint) { + logrus.Debugf("Endpoints(%s) is empty, ignore it", endpointKey) + return false } return true } +func hasReadyAddresses(endpoints *corev1.Endpoints) bool { + for _, ep := range endpoints.Subsets { + if len(ep.Addresses) > 0 { + return true + } + } + return false +} + // GetIngress returns the Ingress matching key. func (s *k8sStore) GetIngress(key string) (*extensions.Ingress, error) { return s.listers.Ingress.ByKey(key) From 480297693f2d6520042a20bd221019fb6f9bc87a Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Mon, 2 Nov 2020 19:48:48 +0800 Subject: [PATCH 8/8] bug fix: not ready ep in /etc/hosts --- discover.v2/k8s_discover.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/discover.v2/k8s_discover.go b/discover.v2/k8s_discover.go index ee4738fe5..2ffc34aa2 100644 --- a/discover.v2/k8s_discover.go +++ b/discover.v2/k8s_discover.go @@ -3,6 +3,7 @@ package discover import ( "context" "errors" + "reflect" "sync" "time" @@ -90,18 +91,24 @@ func (k *k8sDiscover) discover(name string, callback CallbackUpdate) { DeleteFunc: func(obj interface{}) { pod := obj.(*corev1.Pod) ep := endpointForPod(pod) + ep.Mode = 2 callback.UpdateEndpoints(config.DELETE, ep) }, UpdateFunc: func(old, cur interface{}) { oldPod := old.(*corev1.Pod) curPod := cur.(*corev1.Pod) - if oldPod.Status.Phase == curPod.Status.Phase { - return - } - if !isPodReady(curPod) { + + if reflect.DeepEqual(oldPod, curPod) { return } + ep := endpointForPod(curPod) + if !isPodReady(curPod) { + logrus.Infof("unready pod(%s%s) received, delete endpoint based on the pod", curPod.Name, curPod.Namespace) + ep.Mode = 2 + callback.UpdateEndpoints(config.DELETE, ep) + return + } callback.UpdateEndpoints(config.SYNC, ep) }, }