From 8937d6556d79ce92e39578b7ec3e3fcbcb65d645 Mon Sep 17 00:00:00 2001 From: Samra10 Date: Mon, 27 Nov 2023 02:56:24 +0200 Subject: [PATCH 01/11] Add components verify step to bootstraping WGE --- pkg/bootstrap/steps/install_wge.go | 39 +++++++++++ pkg/bootstrap/utils/status-tmp.go | 107 +++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 pkg/bootstrap/utils/status-tmp.go diff --git a/pkg/bootstrap/steps/install_wge.go b/pkg/bootstrap/steps/install_wge.go index a8482de221..74a5c541e6 100644 --- a/pkg/bootstrap/steps/install_wge.go +++ b/pkg/bootstrap/steps/install_wge.go @@ -11,6 +11,8 @@ import ( "github.com/weaveworks/weave-gitops-enterprise/pkg/bootstrap/utils" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/cli-utils/pkg/object" ) const ( @@ -39,6 +41,10 @@ const ( gitopssetsHealthBindAddress = ":8081" ) +var Components = []string{"cluster-controller-manager", + "weave-gitops-enterprise-mccp-cluster-bootstrap-controller", + "weave-gitops-enterprise-mccp-cluster-service"} + var getUserDomain = StepInput{ Name: inUserDomain, Type: stringInput, @@ -146,6 +152,14 @@ func installWge(input []StepInput, c *Config) ([]StepOutput, error) { CommitMsg: wgeHelmReleaseCommitMsg, } + // Wait for the components to be healthy + + c.Logger.Actionf("waiting for components to be healthy") + err = reportComponentsHealth(c, Components, WGEDefaultNamespace, 5*time.Minute) + if err != nil { + return []StepOutput{}, err + } + return []StepOutput{ { Name: wgeHelmrepoFileName, @@ -248,3 +262,28 @@ func isUserDomainEnabled(input []StepInput, c *Config) bool { } return false } + +func reportComponentsHealth(c *Config, componentNames []string, namespace string, timeout time.Duration) error { + // Initialize the status checker + checker, err := utils.NewStatusChecker(c.KubernetesClient, 5*time.Second, timeout, c.Logger) + if err != nil { + return err + } + + // Construct a list of resources to check + var identifiers []object.ObjMetadata + for _, name := range componentNames { + identifiers = append(identifiers, object.ObjMetadata{ + Namespace: namespace, + Name: name, + GroupKind: schema.GroupKind{Group: "apps", Kind: "Deployment"}, + }) + } + + // Perform the health check + if err := checker.Assess(identifiers...); err != nil { + return err + } + + return nil +} diff --git a/pkg/bootstrap/utils/status-tmp.go b/pkg/bootstrap/utils/status-tmp.go new file mode 100644 index 0000000000..95f38baa3f --- /dev/null +++ b/pkg/bootstrap/utils/status-tmp.go @@ -0,0 +1,107 @@ +/* +Copyright 2020, 2021 The Flux authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package utils + +import ( + "context" + "fmt" + "sort" + "strings" + "time" + + "github.com/weaveworks/weave-gitops/pkg/logger" + "sigs.k8s.io/controller-runtime/pkg/client" + k8s_client "sigs.k8s.io/controller-runtime/pkg/client" + + "sigs.k8s.io/cli-utils/pkg/kstatus/polling" + "sigs.k8s.io/cli-utils/pkg/kstatus/polling/aggregator" + "sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector" + "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event" + "sigs.k8s.io/cli-utils/pkg/kstatus/status" + "sigs.k8s.io/cli-utils/pkg/object" +) + +type StatusChecker struct { + pollInterval time.Duration + timeout time.Duration + client client.Client + statusPoller *polling.StatusPoller + logger logger.Logger +} + +func NewStatusChecker(client k8s_client.Client, pollInterval time.Duration, timeout time.Duration, log logger.Logger) (*StatusChecker, error) { + + return &StatusChecker{ + pollInterval: pollInterval, + timeout: timeout, + client: client, + statusPoller: polling.NewStatusPoller(client, client.RESTMapper(), polling.Options{}), + logger: log, + }, nil +} + +func (sc *StatusChecker) Assess(identifiers ...object.ObjMetadata) error { + ctx, cancel := context.WithTimeout(context.Background(), sc.timeout) + defer cancel() + + opts := polling.PollOptions{PollInterval: sc.pollInterval} + eventsChan := sc.statusPoller.Poll(ctx, identifiers, opts) + + coll := collector.NewResourceStatusCollector(identifiers) + done := coll.ListenWithObserver(eventsChan, desiredStatusNotifierFunc(cancel, status.CurrentStatus)) + + <-done + + // we use sorted identifiers to loop over the resource statuses because a Go's map is unordered. + // sorting identifiers by object's name makes sure that the logs look stable for every run + sort.SliceStable(identifiers, func(i, j int) bool { + return strings.Compare(identifiers[i].Name, identifiers[j].Name) < 0 + }) + for _, id := range identifiers { + rs := coll.ResourceStatuses[id] + switch rs.Status { + case status.CurrentStatus: + sc.logger.Successf("%s: %s ready", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind)) + case status.NotFoundStatus: + sc.logger.Failuref("%s: %s not found", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind)) + default: + sc.logger.Failuref("%s: %s not ready", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind)) + } + } + + if coll.Error != nil || ctx.Err() == context.DeadlineExceeded { + return fmt.Errorf("timed out waiting for condition") + } + return nil +} + +// desiredStatusNotifierFunc returns an Observer function for the +// ResourceStatusCollector that will cancel the context (using the cancelFunc) +// when all resources have reached the desired status. +func desiredStatusNotifierFunc(cancelFunc context.CancelFunc, + desired status.Status) collector.ObserverFunc { + return func(rsc *collector.ResourceStatusCollector, _ event.Event) { + var rss []*event.ResourceStatus + for _, rs := range rsc.ResourceStatuses { + rss = append(rss, rs) + } + aggStatus := aggregator.AggregateStatus(rss, desired) + if aggStatus == desired { + cancelFunc() + } + } +} From 24b23172651c6917a69dccdcc0d02e99d93313f8 Mon Sep 17 00:00:00 2001 From: Samra10 Date: Thu, 30 Nov 2023 01:22:25 +0200 Subject: [PATCH 02/11] Add skipComponentCheck to isolate unit_test --- pkg/bootstrap/steps/common_tests.go | 1 + pkg/bootstrap/steps/config.go | 4 ++++ pkg/bootstrap/steps/install_wge.go | 12 ++++++---- pkg/bootstrap/steps/install_wge_test.go | 31 ++++++++++++++----------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/pkg/bootstrap/steps/common_tests.go b/pkg/bootstrap/steps/common_tests.go index d7a6ed18a0..9dd1944b1f 100644 --- a/pkg/bootstrap/steps/common_tests.go +++ b/pkg/bootstrap/steps/common_tests.go @@ -34,5 +34,6 @@ func makeTestConfig(t *testing.T, config Config, objects ...runtime.Object) Conf ClientSecret: config.ClientSecret, RedirectURL: config.RedirectURL, PromptedForDiscoveryURL: config.PromptedForDiscoveryURL, + SkipComponentCheck: config.SkipComponentCheck, } } diff --git a/pkg/bootstrap/steps/config.go b/pkg/bootstrap/steps/config.go index b8321ca74e..2f97425f3e 100644 --- a/pkg/bootstrap/steps/config.go +++ b/pkg/bootstrap/steps/config.go @@ -78,6 +78,7 @@ type ConfigBuilder struct { clientID string clientSecret string PromptedForDiscoveryURL bool + SkipComponentCheck bool } func NewConfigBuilder() *ConfigBuilder { @@ -190,6 +191,8 @@ type Config struct { ClientSecret string RedirectURL string PromptedForDiscoveryURL bool + + SkipComponentCheck bool // skip checking if components are installed } // Builds creates a valid config so boostrap could be executed. It uses values introduced @@ -245,6 +248,7 @@ func (cb *ConfigBuilder) Build() (Config, error) { ClientID: cb.clientID, ClientSecret: cb.clientSecret, PromptedForDiscoveryURL: cb.PromptedForDiscoveryURL, + SkipComponentCheck: cb.SkipComponentCheck, }, nil } diff --git a/pkg/bootstrap/steps/install_wge.go b/pkg/bootstrap/steps/install_wge.go index 74a5c541e6..af367d2f2a 100644 --- a/pkg/bootstrap/steps/install_wge.go +++ b/pkg/bootstrap/steps/install_wge.go @@ -152,12 +152,14 @@ func installWge(input []StepInput, c *Config) ([]StepOutput, error) { CommitMsg: wgeHelmReleaseCommitMsg, } - // Wait for the components to be healthy + if !c.SkipComponentCheck { + // Wait for the components to be healthy - c.Logger.Actionf("waiting for components to be healthy") - err = reportComponentsHealth(c, Components, WGEDefaultNamespace, 5*time.Minute) - if err != nil { - return []StepOutput{}, err + c.Logger.Actionf("waiting for components to be healthy") + err = reportComponentsHealth(c, Components, WGEDefaultNamespace, 5*time.Minute) + if err != nil { + return []StepOutput{}, err + } } return []StepOutput{ diff --git a/pkg/bootstrap/steps/install_wge_test.go b/pkg/bootstrap/steps/install_wge_test.go index 6d4e54a6b0..27f6b9235a 100644 --- a/pkg/bootstrap/steps/install_wge_test.go +++ b/pkg/bootstrap/steps/install_wge_test.go @@ -126,15 +126,17 @@ status: {} func TestInstallWge(t *testing.T) { tests := []struct { - name string - domainType string - input []StepInput - output []StepOutput - err bool + name string + domainType string + skipComponentCheck bool + input []StepInput + output []StepOutput + err bool }{ { - name: "unsupported domain type", - domainType: "wrongType", + name: "unsupported domain type", + domainType: "wrongType", + skipComponentCheck: true, // This should skip the health check input: []StepInput{ { Name: inUserDomain, @@ -144,8 +146,9 @@ func TestInstallWge(t *testing.T) { err: true, }, { - name: "install with domaintype localhost", - domainType: domainTypeLocalhost, + name: "install with domaintype localhost", + domainType: domainTypeLocalhost, + skipComponentCheck: true, // This should skip the health check input: []StepInput{ { Name: inUserDomain, @@ -175,8 +178,9 @@ func TestInstallWge(t *testing.T) { err: false, }, { - name: "install with domaintype external dns", - domainType: domainTypeExternalDNS, + name: "install with domaintype external dns", + domainType: domainTypeExternalDNS, + skipComponentCheck: true, // This should skip the health check input: []StepInput{ { Name: inUserDomain, @@ -210,8 +214,9 @@ func TestInstallWge(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { testConfig := Config{ - WGEVersion: "1.0.0", - DomainType: tt.domainType, + WGEVersion: "1.0.0", + DomainType: tt.domainType, + SkipComponentCheck: tt.skipComponentCheck, } config := makeTestConfig(t, testConfig) From 171dc77d46417a5b5a92aa272979573437d4a0fb Mon Sep 17 00:00:00 2001 From: Samra10 Date: Thu, 30 Nov 2023 01:28:57 +0200 Subject: [PATCH 03/11] rename status.go file --- pkg/bootstrap/utils/{status-tmp.go => status.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pkg/bootstrap/utils/{status-tmp.go => status.go} (100%) diff --git a/pkg/bootstrap/utils/status-tmp.go b/pkg/bootstrap/utils/status.go similarity index 100% rename from pkg/bootstrap/utils/status-tmp.go rename to pkg/bootstrap/utils/status.go From 6eb14246ba275c088fe31bb8786d79339fc1aae2 Mon Sep 17 00:00:00 2001 From: Samra10 Date: Thu, 30 Nov 2023 02:50:45 +0200 Subject: [PATCH 04/11] add integration test for status checker --- pkg/bootstrap/utils/status.go | 16 ---- .../utils/status_integration_test.go | 79 +++++++++++++++++++ 2 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 pkg/bootstrap/utils/status_integration_test.go diff --git a/pkg/bootstrap/utils/status.go b/pkg/bootstrap/utils/status.go index 95f38baa3f..453656c93f 100644 --- a/pkg/bootstrap/utils/status.go +++ b/pkg/bootstrap/utils/status.go @@ -1,19 +1,3 @@ -/* -Copyright 2020, 2021 The Flux authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - package utils import ( diff --git a/pkg/bootstrap/utils/status_integration_test.go b/pkg/bootstrap/utils/status_integration_test.go new file mode 100644 index 0000000000..95271605b7 --- /dev/null +++ b/pkg/bootstrap/utils/status_integration_test.go @@ -0,0 +1,79 @@ +//go:build integration + +package utils + +import ( + "fmt" + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + testutils "github.com/weaveworks/weave-gitops-enterprise/test/utils" + "github.com/weaveworks/weave-gitops/pkg/logger" + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/cli-utils/pkg/object" + k8s_client "sigs.k8s.io/controller-runtime/pkg/client" + k8s_config "sigs.k8s.io/controller-runtime/pkg/client/config" +) + +func TestStatusCheckerIt(t *testing.T) { + // Setup function to create kubeconfig and set the KUBECONFIG environment variable + setup := func() error { + kp, err := testutils.CreateKubeconfigFileForRestConfig(*cfg) + if err != nil { + return fmt.Errorf("cannot create kubeconfig: %w", err) + } + os.Setenv("KUBECONFIG", kp) + return nil + } + + // Reset function to clean up after the test + reset := func() { + os.Unsetenv("KUBECONFIG") + } + + // Run the setup function and handle errors + assert.NoError(t, setup(), "Error setting up kubeconfig") + defer reset() + + // Create the Kubernetes client using the kubeconfig + config, err := k8s_config.GetConfig() + assert.NoError(t, err, "Error getting Kubernetes config") + client, err := k8s_client.New(config, k8s_client.Options{}) + assert.NoError(t, err, "Error creating Kubernetes client") + + // Initialize logger + logInstance := logger.NewCLILogger(os.Stdout) + + // Initialize the StatusChecker + statusChecker, err := NewStatusChecker(client, 5*time.Second, 1*time.Minute, logInstance) + assert.NoError(t, err, "Error creating StatusChecker") + + // Define test cases + testCases := []struct { + name string + identifiers []object.ObjMetadata + }{ + { + name: "Check specific Kubernetes resource", + identifiers: []object.ObjMetadata{ + { + Name: "cluster-controller-manager", + Namespace: "flux-system", + GroupKind: schema.GroupKind{Group: "apps", Kind: "Deployment"}, + }, + // Add more resources as needed + }, + }, + // ... other test cases ... + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // Call the Assess function to check the status of resources + err := statusChecker.Assess(tc.identifiers...) + assert.NoError(t, err, "Error assessing resource status") + }) + } +} From fcdb1a27a0385607ebefe3f55d253eda064b4146 Mon Sep 17 00:00:00 2001 From: Samra10 Date: Tue, 5 Dec 2023 13:13:30 +0200 Subject: [PATCH 05/11] update verify step log type --- pkg/bootstrap/steps/install_wge.go | 2 +- pkg/bootstrap/utils/status_integration_test.go | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/bootstrap/steps/install_wge.go b/pkg/bootstrap/steps/install_wge.go index af367d2f2a..0302739e0c 100644 --- a/pkg/bootstrap/steps/install_wge.go +++ b/pkg/bootstrap/steps/install_wge.go @@ -155,7 +155,7 @@ func installWge(input []StepInput, c *Config) ([]StepOutput, error) { if !c.SkipComponentCheck { // Wait for the components to be healthy - c.Logger.Actionf("waiting for components to be healthy") + c.Logger.Waitingf("waiting for components to be healthy") err = reportComponentsHealth(c, Components, WGEDefaultNamespace, 5*time.Minute) if err != nil { return []StepOutput{}, err diff --git a/pkg/bootstrap/utils/status_integration_test.go b/pkg/bootstrap/utils/status_integration_test.go index 95271605b7..2183fd3e2b 100644 --- a/pkg/bootstrap/utils/status_integration_test.go +++ b/pkg/bootstrap/utils/status_integration_test.go @@ -63,10 +63,8 @@ func TestStatusCheckerIt(t *testing.T) { Namespace: "flux-system", GroupKind: schema.GroupKind{Group: "apps", Kind: "Deployment"}, }, - // Add more resources as needed }, }, - // ... other test cases ... } for _, tc := range testCases { From ef56b7f7d579db6b09ee2040cab5a0a2e747c6be Mon Sep 17 00:00:00 2001 From: Samra10 Date: Tue, 5 Dec 2023 13:22:49 +0200 Subject: [PATCH 06/11] add components health to install WGE step --- pkg/bootstrap/steps/install_wge.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/bootstrap/steps/install_wge.go b/pkg/bootstrap/steps/install_wge.go index 8940574cfa..e804eaed85 100644 --- a/pkg/bootstrap/steps/install_wge.go +++ b/pkg/bootstrap/steps/install_wge.go @@ -11,6 +11,8 @@ import ( "github.com/weaveworks/weave-gitops-enterprise/pkg/bootstrap/utils" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/cli-utils/pkg/object" ) const ( @@ -249,3 +251,28 @@ func constructWGEhelmRelease(valuesFile valuesFile, chartVersion string) (string return utils.CreateHelmReleaseYamlString(wgeHelmRelease) } + +func reportComponentsHealth(c *Config, componentNames []string, namespace string, timeout time.Duration) error { + // Initialize the status checker + checker, err := utils.NewStatusChecker(c.KubernetesClient, 5*time.Second, timeout, c.Logger) + if err != nil { + return err + } + + // Construct a list of resources to check + var identifiers []object.ObjMetadata + for _, name := range componentNames { + identifiers = append(identifiers, object.ObjMetadata{ + Namespace: namespace, + Name: name, + GroupKind: schema.GroupKind{Group: "apps", Kind: "Deployment"}, + }) + } + + // Perform the health check + if err := checker.Assess(identifiers...); err != nil { + return err + } + + return nil +} From 9762361f04470d8be0c58b40d7c022615d2c308e Mon Sep 17 00:00:00 2001 From: Samra10 Date: Tue, 5 Dec 2023 23:27:54 +0200 Subject: [PATCH 07/11] Extend step to add verification logic --- pkg/bootstrap/steps/install_wge.go | 30 +++++++++++++++++------------- pkg/bootstrap/steps/step.go | 17 +++++++++++++---- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/pkg/bootstrap/steps/install_wge.go b/pkg/bootstrap/steps/install_wge.go index e804eaed85..2339fa83ef 100644 --- a/pkg/bootstrap/steps/install_wge.go +++ b/pkg/bootstrap/steps/install_wge.go @@ -39,6 +39,7 @@ const ( ) var Components = []string{"cluster-controller-manager", + "gitopssets-controller-manager", "weave-gitops-enterprise-mccp-cluster-bootstrap-controller", "weave-gitops-enterprise-mccp-cluster-service"} @@ -47,9 +48,10 @@ func NewInstallWGEStep() BootstrapStep { inputs := []StepInput{} return BootstrapStep{ - Name: "Install Weave GitOps Enterprise", - Input: inputs, - Step: installWge, + Name: "Install Weave GitOps Enterprise", + Input: inputs, + Step: installWge, + Verify: verifyComponents, } } @@ -117,16 +119,6 @@ func installWge(input []StepInput, c *Config) ([]StepOutput, error) { CommitMsg: wgeHelmReleaseCommitMsg, } - if !c.SkipComponentCheck { - // Wait for the components to be healthy - - c.Logger.Waitingf("waiting for components to be healthy") - err = reportComponentsHealth(c, Components, WGEDefaultNamespace, 5*time.Minute) - if err != nil { - return []StepOutput{}, err - } - } - return []StepOutput{ { Name: wgeHelmrepoFileName, @@ -252,6 +244,18 @@ func constructWGEhelmRelease(valuesFile valuesFile, chartVersion string) (string return utils.CreateHelmReleaseYamlString(wgeHelmRelease) } +//verifycomponents func(input []StepInput, c *Config) error +// which use reportComponentsHealth func + +func verifyComponents(input []StepInput, c *Config) error { + c.Logger.Waitingf("waiting for components to be healthy") + err := reportComponentsHealth(c, Components, WGEDefaultNamespace, 5*time.Minute) + if err != nil { + return err + } + return nil +} + func reportComponentsHealth(c *Config, componentNames []string, namespace string, timeout time.Duration) error { // Initialize the status checker checker, err := utils.NewStatusChecker(c.KubernetesClient, 5*time.Second, timeout, c.Logger) diff --git a/pkg/bootstrap/steps/step.go b/pkg/bootstrap/steps/step.go index fd65221f1d..09ed93f463 100644 --- a/pkg/bootstrap/steps/step.go +++ b/pkg/bootstrap/steps/step.go @@ -13,10 +13,11 @@ import ( // It is abstracted to have a generic way to handle them, so we could achieve easier // extensibility, consistency and maintainability. type BootstrapStep struct { - Name string - Input []StepInput - Step func(input []StepInput, c *Config) ([]StepOutput, error) - Stdin io.ReadCloser + Name string + Input []StepInput + Step func(input []StepInput, c *Config) ([]StepOutput, error) + Verify func(input []StepInput, c *Config) error + Stdin io.ReadCloser } // StepInput represents an input a step requires to execute it. for example user needs to introduce a string or a password. @@ -76,6 +77,14 @@ func (s BootstrapStep) Execute(c *Config) ([]StepOutput, error) { if err != nil { return []StepOutput{}, fmt.Errorf("cannot process output '%s': %v", s.Name, err) } + + //verify the result of the step if the function is defined in the step + if s.Verify != nil { + if err := s.Verify(inputValues, c); err != nil { + return []StepOutput{}, fmt.Errorf("cannot verify '%s': %v", s.Name, err) + } + } + return outputs, nil } From 749bbfc430c46ebdabdf953c0d1e467fe0948fc2 Mon Sep 17 00:00:00 2001 From: Samra10 Date: Wed, 6 Dec 2023 00:15:44 +0200 Subject: [PATCH 08/11] remove unnecessary flag --- pkg/bootstrap/steps/common_tests.go | 1 - pkg/bootstrap/steps/config.go | 4 ---- pkg/bootstrap/steps/install_wge_test.go | 3 ++- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/bootstrap/steps/common_tests.go b/pkg/bootstrap/steps/common_tests.go index eecc157d19..e7be569733 100644 --- a/pkg/bootstrap/steps/common_tests.go +++ b/pkg/bootstrap/steps/common_tests.go @@ -56,6 +56,5 @@ func makeTestConfig(t *testing.T, config Config, objects ...runtime.Object) Conf ClientSecret: config.ClientSecret, RedirectURL: config.RedirectURL, PromptedForDiscoveryURL: config.PromptedForDiscoveryURL, - SkipComponentCheck: config.SkipComponentCheck, } } diff --git a/pkg/bootstrap/steps/config.go b/pkg/bootstrap/steps/config.go index 5aa595ed98..23e65d56c2 100644 --- a/pkg/bootstrap/steps/config.go +++ b/pkg/bootstrap/steps/config.go @@ -73,7 +73,6 @@ type ConfigBuilder struct { clientID string clientSecret string PromptedForDiscoveryURL bool - SkipComponentCheck bool } func NewConfigBuilder() *ConfigBuilder { @@ -174,8 +173,6 @@ type Config struct { ClientSecret string RedirectURL string PromptedForDiscoveryURL bool - - SkipComponentCheck bool // skip checking if components are installed } // Builds creates a valid config so boostrap could be executed. It uses values introduced @@ -230,7 +227,6 @@ func (cb *ConfigBuilder) Build() (Config, error) { ClientID: cb.clientID, ClientSecret: cb.clientSecret, PromptedForDiscoveryURL: cb.PromptedForDiscoveryURL, - SkipComponentCheck: cb.SkipComponentCheck, }, nil } diff --git a/pkg/bootstrap/steps/install_wge_test.go b/pkg/bootstrap/steps/install_wge_test.go index da323798ba..22454b9e03 100644 --- a/pkg/bootstrap/steps/install_wge_test.go +++ b/pkg/bootstrap/steps/install_wge_test.go @@ -122,7 +122,6 @@ func TestInstallWge_Execute(t *testing.T) { Path: "/", Scheme: "https", }, - SkipComponentCheck: true, }, fluxSystemGitRepository(), fluxSystemKustomization()), wantOutput: []StepOutput{ { @@ -149,6 +148,8 @@ func TestInstallWge_Execute(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { step := NewInstallWGEStep() + // skip verify step + step.Verify = nil gotOutputs, err := step.Execute(&tt.config) if tt.wantErr != "" { if msg := err.Error(); msg != tt.wantErr { From eabd44413046b147b14d8b507d88a05bc65bfc76 Mon Sep 17 00:00:00 2001 From: Samra10 Date: Wed, 6 Dec 2023 14:08:19 +0200 Subject: [PATCH 09/11] update verification step param --- pkg/bootstrap/steps/install_wge.go | 5 +---- pkg/bootstrap/steps/step.go | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/pkg/bootstrap/steps/install_wge.go b/pkg/bootstrap/steps/install_wge.go index 2339fa83ef..26acdada10 100644 --- a/pkg/bootstrap/steps/install_wge.go +++ b/pkg/bootstrap/steps/install_wge.go @@ -244,10 +244,7 @@ func constructWGEhelmRelease(valuesFile valuesFile, chartVersion string) (string return utils.CreateHelmReleaseYamlString(wgeHelmRelease) } -//verifycomponents func(input []StepInput, c *Config) error -// which use reportComponentsHealth func - -func verifyComponents(input []StepInput, c *Config) error { +func verifyComponents(output []StepOutput, c *Config) error { c.Logger.Waitingf("waiting for components to be healthy") err := reportComponentsHealth(c, Components, WGEDefaultNamespace, 5*time.Minute) if err != nil { diff --git a/pkg/bootstrap/steps/step.go b/pkg/bootstrap/steps/step.go index 09ed93f463..a7837306b2 100644 --- a/pkg/bootstrap/steps/step.go +++ b/pkg/bootstrap/steps/step.go @@ -16,7 +16,7 @@ type BootstrapStep struct { Name string Input []StepInput Step func(input []StepInput, c *Config) ([]StepOutput, error) - Verify func(input []StepInput, c *Config) error + Verify func(output []StepOutput, c *Config) error Stdin io.ReadCloser } @@ -80,7 +80,7 @@ func (s BootstrapStep) Execute(c *Config) ([]StepOutput, error) { //verify the result of the step if the function is defined in the step if s.Verify != nil { - if err := s.Verify(inputValues, c); err != nil { + if err := s.Verify(outputs, c); err != nil { return []StepOutput{}, fmt.Errorf("cannot verify '%s': %v", s.Name, err) } } From fe93e2caaebc4d7efe9caffe290f99cacdc1875c Mon Sep 17 00:00:00 2001 From: Samra10 Date: Wed, 6 Dec 2023 14:22:55 +0200 Subject: [PATCH 10/11] undo -integration --- .../utils/status_integration_test.go | 77 ------------------- 1 file changed, 77 deletions(-) delete mode 100644 pkg/bootstrap/utils/status_integration_test.go diff --git a/pkg/bootstrap/utils/status_integration_test.go b/pkg/bootstrap/utils/status_integration_test.go deleted file mode 100644 index 2183fd3e2b..0000000000 --- a/pkg/bootstrap/utils/status_integration_test.go +++ /dev/null @@ -1,77 +0,0 @@ -//go:build integration - -package utils - -import ( - "fmt" - "os" - "testing" - "time" - - "github.com/stretchr/testify/assert" - testutils "github.com/weaveworks/weave-gitops-enterprise/test/utils" - "github.com/weaveworks/weave-gitops/pkg/logger" - "k8s.io/apimachinery/pkg/runtime/schema" - "sigs.k8s.io/cli-utils/pkg/object" - k8s_client "sigs.k8s.io/controller-runtime/pkg/client" - k8s_config "sigs.k8s.io/controller-runtime/pkg/client/config" -) - -func TestStatusCheckerIt(t *testing.T) { - // Setup function to create kubeconfig and set the KUBECONFIG environment variable - setup := func() error { - kp, err := testutils.CreateKubeconfigFileForRestConfig(*cfg) - if err != nil { - return fmt.Errorf("cannot create kubeconfig: %w", err) - } - os.Setenv("KUBECONFIG", kp) - return nil - } - - // Reset function to clean up after the test - reset := func() { - os.Unsetenv("KUBECONFIG") - } - - // Run the setup function and handle errors - assert.NoError(t, setup(), "Error setting up kubeconfig") - defer reset() - - // Create the Kubernetes client using the kubeconfig - config, err := k8s_config.GetConfig() - assert.NoError(t, err, "Error getting Kubernetes config") - client, err := k8s_client.New(config, k8s_client.Options{}) - assert.NoError(t, err, "Error creating Kubernetes client") - - // Initialize logger - logInstance := logger.NewCLILogger(os.Stdout) - - // Initialize the StatusChecker - statusChecker, err := NewStatusChecker(client, 5*time.Second, 1*time.Minute, logInstance) - assert.NoError(t, err, "Error creating StatusChecker") - - // Define test cases - testCases := []struct { - name string - identifiers []object.ObjMetadata - }{ - { - name: "Check specific Kubernetes resource", - identifiers: []object.ObjMetadata{ - { - Name: "cluster-controller-manager", - Namespace: "flux-system", - GroupKind: schema.GroupKind{Group: "apps", Kind: "Deployment"}, - }, - }, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - // Call the Assess function to check the status of resources - err := statusChecker.Assess(tc.identifiers...) - assert.NoError(t, err, "Error assessing resource status") - }) - } -} From 90f4cc2f796e1011e12c8e3d05d7fadc53e460d2 Mon Sep 17 00:00:00 2001 From: Samra10 Date: Thu, 7 Dec 2023 13:36:46 +0200 Subject: [PATCH 11/11] Add comments for Exported methods --- pkg/bootstrap/utils/status.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/bootstrap/utils/status.go b/pkg/bootstrap/utils/status.go index 453656c93f..c9e7a0170b 100644 --- a/pkg/bootstrap/utils/status.go +++ b/pkg/bootstrap/utils/status.go @@ -19,6 +19,8 @@ import ( "sigs.k8s.io/cli-utils/pkg/object" ) +// StatusChecker is a wrapper around the StatusPoller +// that provides a way to poll the status of a set of resources type StatusChecker struct { pollInterval time.Duration timeout time.Duration @@ -27,6 +29,7 @@ type StatusChecker struct { logger logger.Logger } +// NewStatusChecker returns a new StatusChecker that will use the provided client to poll the status of the resources func NewStatusChecker(client k8s_client.Client, pollInterval time.Duration, timeout time.Duration, log logger.Logger) (*StatusChecker, error) { return &StatusChecker{ @@ -38,6 +41,7 @@ func NewStatusChecker(client k8s_client.Client, pollInterval time.Duration, time }, nil } +// Assess will poll the status of the provided resources until all resources have reached the desired status func (sc *StatusChecker) Assess(identifiers ...object.ObjMetadata) error { ctx, cancel := context.WithTimeout(context.Background(), sc.timeout) defer cancel()