From a74fb181b352da54eac283f6dbbd4c96c3f76002 Mon Sep 17 00:00:00 2001 From: qiaozp <47812250+chivalryq@users.noreply.github.com> Date: Mon, 14 Nov 2022 11:43:23 +0800 Subject: [PATCH] Chore: remove legacy o11y logic (#5046) Signed-off-by: Qiaozp Signed-off-by: Qiaozp --- pkg/addon/addon.go | 90 ------------------------ pkg/addon/addon_test.go | 149 ---------------------------------------- pkg/addon/helper.go | 95 +------------------------ 3 files changed, 1 insertion(+), 333 deletions(-) diff --git a/pkg/addon/addon.go b/pkg/addon/addon.go index e422546ed..70b28a852 100644 --- a/pkg/addon/addon.go +++ b/pkg/addon/addon.go @@ -29,7 +29,6 @@ import ( "path/filepath" "strings" "sync" - "text/template" "time" "github.com/Masterminds/semver/v3" @@ -64,7 +63,6 @@ import ( "github.com/oam-dev/kubevela/pkg/config" "github.com/oam-dev/kubevela/pkg/cue/script" "github.com/oam-dev/kubevela/pkg/definition" - "github.com/oam-dev/kubevela/pkg/multicluster" "github.com/oam-dev/kubevela/pkg/oam" "github.com/oam-dev/kubevela/pkg/oam/util" "github.com/oam-dev/kubevela/pkg/utils" @@ -142,66 +140,12 @@ var ( ) const ( - // ObservabilityAddon is the name of the observability addon - ObservabilityAddon = "observability" - // ObservabilityAddonEndpointComponent is the endpoint component name of the observability addon - ObservabilityAddonEndpointComponent = "grafana" - // ObservabilityAddonDomainArg is the domain argument name of the observability addon - ObservabilityAddonDomainArg = "domain" // LocalAddonRegistryName is the addon-registry name for those installed by local dir LocalAddonRegistryName = "local" // ClusterLabelSelector define the key of topology cluster label selector ClusterLabelSelector = "clusterLabelSelector" ) -// ObservabilityEnvironment contains the Observability addon's domain for each cluster -type ObservabilityEnvironment struct { - Cluster string - Domain string - LoadBalancerIP string - ServiceExternalIP string -} - -// ObservabilityEnvBindingValues is a list of ObservabilityEnvironment and will be used to render observability-env-binding.yaml -type ObservabilityEnvBindingValues struct { - Envs []ObservabilityEnvironment -} - -const ( - // ObservabilityEnvBindingEnvTag is the env Tag for env-binding settings for observability addon - ObservabilityEnvBindingEnvTag = ` envs:` - - // ObservabilityEnvBindingEnvTmpl is the env values for env-binding settings for observability addon - ObservabilityEnvBindingEnvTmpl = ` - {{ with .Envs}} - {{ range . }} - - name: {{.Cluster}} - placement: - clusterSelector: - name: {{.Cluster}} - {{ end }} - {{ end }}` - - // ObservabilityWorkflowStepsTag is the workflow steps Tag for observability addon - ObservabilityWorkflowStepsTag = `steps:` - - // ObservabilityWorkflow4EnvBindingTmpl is the workflow for env-binding settings for observability addon - ObservabilityWorkflow4EnvBindingTmpl = ` -{{ with .Envs}} - {{ range . }} - - name: {{ .Cluster }} - type: deploy2env - properties: - policy: domain - env: {{ .Cluster }} - parallel: true - {{ end }} -{{ end }}` -) - -// ErrorNoDomain is the error when no domain is found -var ErrorNoDomain = errors.New("domain is not set") - // Pattern indicates the addon framework file pattern, all files should match at least one of the pattern. type Pattern struct { IsDir bool @@ -770,40 +714,6 @@ func RenderViews(addon *InstallPackage) ([]*unstructured.Unstructured, error) { return views, nil } -func allocateDomainForAddon(ctx context.Context, k8sClient client.Client) ([]ObservabilityEnvironment, error) { - secrets, err := multicluster.ListExistingClusterSecrets(ctx, k8sClient) - if err != nil { - klog.Error(err, "failed to list existing cluster secrets") - return nil, err - } - - envs := make([]ObservabilityEnvironment, len(secrets)) - - for i, secret := range secrets { - cluster := secret.Name - envs[i] = ObservabilityEnvironment{ - Cluster: cluster, - } - } - - return envs, nil -} - -func render(envs []ObservabilityEnvironment, tmpl string) (string, error) { - todos := ObservabilityEnvBindingValues{ - Envs: envs, - } - - t := template.Must(template.New("grafana").Parse(tmpl)) - var rendered bytes.Buffer - err := t.Execute(&rendered, todos) - if err != nil { - return "", err - } - - return rendered.String(), nil -} - func renderObject(elem ElementFile) (*unstructured.Unstructured, error) { obj := &unstructured.Unstructured{} dec := k8syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme) diff --git a/pkg/addon/addon_test.go b/pkg/addon/addon_test.go index 0a35ee784..2450174ba 100644 --- a/pkg/addon/addon_test.go +++ b/pkg/addon/addon_test.go @@ -38,14 +38,10 @@ import ( corev1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - v1alpha12 "github.com/oam-dev/cluster-gateway/pkg/apis/cluster/v1alpha1" - clustercommon "github.com/oam-dev/cluster-gateway/pkg/common" - "github.com/oam-dev/kubevela/apis/core.oam.dev/common" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" @@ -207,81 +203,6 @@ func TestGetAddonData(t *testing.T) { testReaderFunc(t, reader) } -func TestRender(t *testing.T) { - testcases := []struct { - envs []ObservabilityEnvironment - tmpl string - expect string - err error - }{ - { - envs: []ObservabilityEnvironment{ - { - Cluster: "c1", - }, - { - Cluster: "c2", - }, - }, - tmpl: ObservabilityEnvBindingEnvTmpl, - expect: ` - - - - name: c1 - placement: - clusterSelector: - name: c1 - - - name: c2 - placement: - clusterSelector: - name: c2 - - `, - - err: nil, - }, - { - envs: []ObservabilityEnvironment{ - { - Cluster: "c1", - }, - { - Cluster: "c2", - }, - }, - tmpl: ObservabilityWorkflow4EnvBindingTmpl, - expect: ` - - - - name: c1 - type: deploy2env - properties: - policy: domain - env: c1 - parallel: true - - - name: c2 - type: deploy2env - properties: - policy: domain - env: c2 - parallel: true - -`, - - err: nil, - }, - } - for _, tc := range testcases { - t.Run("", func(t *testing.T) { - rendered, err := render(tc.envs, tc.tmpl) - assert.Equal(t, tc.err, err) - assert.Equal(t, tc.expect, rendered) - }) - } -} - func TestRenderApp(t *testing.T) { addon := baseAddon app, _, err := RenderApp(ctx, &addon, nil, map[string]interface{}{}) @@ -454,76 +375,6 @@ func TestGetAddonStatus(t *testing.T) { } } -func TestGetAddonStatus4Observability(t *testing.T) { - ctx := context.Background() - - addonApplication := &v1beta1.Application{ - ObjectMeta: metav1.ObjectMeta{ - Name: "observability", - Namespace: types.DefaultKubeVelaNS, - }, - Status: common.AppStatus{ - Phase: common.ApplicationRunning, - }, - } - - addonSecret := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: addonutil.Addon2SecName(ObservabilityAddon), - Namespace: types.DefaultKubeVelaNS, - }, - Data: map[string][]byte{}, - } - - addonService := &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: types.DefaultKubeVelaNS, - Name: ObservabilityAddonEndpointComponent, - }, - Status: corev1.ServiceStatus{ - LoadBalancer: corev1.LoadBalancerStatus{ - Ingress: []corev1.LoadBalancerIngress{ - { - IP: "1.2.3.4", - }, - }, - }, - }, - } - - clusterSecret := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-secret", - Labels: map[string]string{ - clustercommon.LabelKeyClusterCredentialType: string(v1alpha12.CredentialTypeX509Certificate), - }, - }, - Data: map[string][]byte{ - "test-key": []byte("test-value"), - }, - } - - scheme := runtime.NewScheme() - assert.NoError(t, v1beta1.AddToScheme(scheme)) - assert.NoError(t, corev1.AddToScheme(scheme)) - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(addonApplication, addonSecret).Build() - addonStatus, err := GetAddonStatus(context.Background(), k8sClient, ObservabilityAddon) - assert.NoError(t, err) - assert.Equal(t, addonStatus.AddonPhase, enabling) - - // Addon is not installed in multiple clusters - k8sClient = fake.NewClientBuilder().WithScheme(scheme).WithObjects(addonApplication, addonSecret, addonService).Build() - addonStatus, err = GetAddonStatus(context.Background(), k8sClient, ObservabilityAddon) - assert.NoError(t, err) - assert.Equal(t, addonStatus.AddonPhase, enabled) - - // Addon is installed in multiple clusters - assert.NoError(t, k8sClient.Create(ctx, clusterSecret)) - addonStatus, err = GetAddonStatus(context.Background(), k8sClient, ObservabilityAddon) - assert.NoError(t, err) - assert.Equal(t, addonStatus.AddonPhase, enabled) -} - func TestGetAddonVersionMeetSystemRequirement(t *testing.T) { server := httptest.NewServer(helmHandler) defer server.Close() diff --git a/pkg/addon/helper.go b/pkg/addon/helper.go index e93dc122e..ef7514a46 100644 --- a/pkg/addon/helper.go +++ b/pkg/addon/helper.go @@ -18,16 +18,12 @@ package addon import ( "context" - "encoding/json" "fmt" "path/filepath" - "k8s.io/client-go/discovery" - "k8s.io/klog/v2" - v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/client-go/discovery" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" @@ -181,45 +177,6 @@ func GetAddonStatus(ctx context.Context, cli client.Client, name string) (Status switch app.Status.Phase { case commontypes.ApplicationRunning: addonStatus.AddonPhase = enabled - if name == ObservabilityAddon { - // TODO(wonderflow): this is a hack Implementation and need be fixed in a unified way - var ( - sec v1.Secret - domain string - ) - if err = cli.Get(ctx, client.ObjectKey{Namespace: types.DefaultKubeVelaNS, Name: addonutil.Addon2SecName(name)}, &sec); err != nil { - klog.ErrorS(err, "failed to get observability secret") - addonStatus.AddonPhase = enabling - addonStatus.InstalledVersion = "" - return addonStatus, nil - } - - if v, ok := sec.Data[ObservabilityAddonDomainArg]; ok { - domain = string(v) - } - observability, err := GetObservabilityAccessibilityInfo(ctx, cli, domain) - if err != nil { - klog.ErrorS(err, "failed to get observability accessibility info") - addonStatus.AddonPhase = enabling - addonStatus.InstalledVersion = "" - return addonStatus, nil - } - - for _, o := range observability { - var access = fmt.Sprintf("No loadBalancer found, visiting by using 'vela port-forward %s", ObservabilityAddon) - if o.LoadBalancerIP != "" { - access = fmt.Sprintf("Visiting URL: %s, IP: %s", o.Domain, o.LoadBalancerIP) - } - clusters[o.Cluster] = map[string]interface{}{ - "domain": o.Domain, - "loadBalancerIP": o.LoadBalancerIP, - "access": access, - "serviceExternalIP": o.ServiceExternalIP, - } - } - - return addonStatus, nil - } return addonStatus, nil case commontypes.ApplicationDeleting: addonStatus.AddonPhase = disabling @@ -230,56 +187,6 @@ func GetAddonStatus(ctx context.Context, cli client.Client, name string) (Status } } -// GetObservabilityAccessibilityInfo will get the accessibility info of addon in local cluster and multiple clusters -func GetObservabilityAccessibilityInfo(ctx context.Context, k8sClient client.Client, domain string) ([]ObservabilityEnvironment, error) { - domains, err := allocateDomainForAddon(ctx, k8sClient) - if err != nil { - return nil, err - } - - obj := new(unstructured.Unstructured) - obj.SetKind("Service") - obj.SetAPIVersion("v1") - key := client.ObjectKeyFromObject(obj) - key.Namespace = types.DefaultKubeVelaNS - key.Name = ObservabilityAddonEndpointComponent - for i, d := range domains { - if err != nil { - return nil, err - } - readCtx := multicluster.ContextWithClusterName(ctx, d.Cluster) - if err := k8sClient.Get(readCtx, key, obj); err != nil { - return nil, err - } - var svc v1.Service - data, err := obj.MarshalJSON() - if err != nil { - return nil, err - } - if err := json.Unmarshal(data, &svc); err != nil { - return nil, err - } - if svc.Status.LoadBalancer.Ingress != nil && len(svc.Status.LoadBalancer.Ingress) == 1 { - domains[i].ServiceExternalIP = svc.Status.LoadBalancer.Ingress[0].IP - } - } - // set domain for the cluster if there is no child clusters - if len(domains) == 0 { - var svc v1.Service - if err := k8sClient.Get(ctx, client.ObjectKey{Name: ObservabilityAddonEndpointComponent, Namespace: types.DefaultKubeVelaNS}, &svc); err != nil { - return nil, err - } - if svc.Status.LoadBalancer.Ingress != nil && len(svc.Status.LoadBalancer.Ingress) == 1 { - domains = []ObservabilityEnvironment{ - { - ServiceExternalIP: svc.Status.LoadBalancer.Ingress[0].IP, - }, - } - } - } - return domains, nil -} - // FindWholeAddonPackagesFromRegistry find addons' WholeInstallPackage from registries, empty registryName indicates matching all func FindWholeAddonPackagesFromRegistry(ctx context.Context, k8sClient client.Client, addonNames []string, registryNames []string) ([]*WholeAddonPackage, error) { var addons []*WholeAddonPackage