Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verification step to WGE bootstrap #3732

Open
wants to merge 1 commit into
base: 3647-add-verify-step
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 9 additions & 32 deletions pkg/bootstrap/steps/install_wge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ 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 (
Expand All @@ -38,18 +36,13 @@ const (
gitopssetsHealthBindAddress = ":8081"
)

var Components = []string{"cluster-controller-manager",
"gitopssets-controller-manager",
"weave-gitops-enterprise-mccp-cluster-bootstrap-controller",
"weave-gitops-enterprise-mccp-cluster-service"}

// NewInstallWGEStep step to install Weave GitOps Enterprise
func NewInstallWGEStep() BootstrapStep {
return BootstrapStep{
Name: "Install Weave GitOps Enterprise",
Input: []StepInput{},
Step: installWge,
Verify: verifyComponents,
Verify: verifyHelmsRelease,
}
}

Expand Down Expand Up @@ -242,36 +235,20 @@ func constructWGEhelmRelease(valuesFile valuesFile, chartVersion string) (string
return utils.CreateHelmReleaseYamlString(wgeHelmRelease)
}

func verifyComponents(output []StepOutput, c *Config) error {
func verifyHelmsRelease(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 {
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)
isReady, err := utils.CheckHelmReleaseReady(c.KubernetesClient, "weave-gitops-enterprise", "flux-system", 5*time.Minute)
if err != nil {
c.Logger.Failuref("Error checking HelmRelease status: %w", err)
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
if isReady {
c.Logger.Actionf("HelmRelease is ready and components are healthy")
return nil
}

return nil
// Handle the case where the HelmRelease is not ready
return fmt.Errorf("HelmRelease is not ready")
}
46 changes: 44 additions & 2 deletions pkg/bootstrap/utils/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ import (
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/weaveworks/weave-gitops/pkg/runner"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

k8s_client "sigs.k8s.io/controller-runtime/pkg/client"
k8syaml "sigs.k8s.io/yaml"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/controller-runtime/pkg/client"
"time"
)

const (
HelmVersionProperty = "version"
HelmDomainProperty = "domain"
HelmVersionProperty = "version"
HelmDomainProperty = "domain"
HelmReleaseReadyCondition = "Ready"
HelmReleaseReadyConditionTrue = "True"
HelmReleaseReadyConditionFalse = "False"
)

type FluxClient interface {
Expand Down Expand Up @@ -186,3 +196,35 @@ func GetHelmReleaseValues(client k8s_client.Client, name string, namespace strin

return helmrelease.Spec.Values.Raw, nil
}

// CheckHelmReleaseReady checks if the 'Ready' condition of a HelmRelease is true
// and waits until it's ready or the timeout is reached
func CheckHelmReleaseReady(k8sClient client.Client, releaseName, namespace string, timeout time.Duration) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

ticker := time.NewTicker(5 * time.Second) // Polling interval
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return false, fmt.Errorf("timeout waiting for HelmRelease %s to be ready", releaseName)
case <-ticker.C:
helmRelease := &helmv2.HelmRelease{}
err := k8sClient.Get(ctx, client.ObjectKey{
Name: releaseName,
Namespace: namespace,
}, helmRelease)
if err != nil {
return false, fmt.Errorf("error getting HelmRelease: %w", err)
}

for _, condition := range helmRelease.Status.Conditions {
if condition.Type == HelmReleaseReadyCondition && condition.Status == metav1.ConditionTrue {
return true, nil
}
}
}
}
}
60 changes: 60 additions & 0 deletions pkg/bootstrap/utils/flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
sourcev1beta2 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/weaveworks/weave-gitops-enterprise/test/utils"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -290,3 +291,62 @@ func TestGetHelmReleaseValues(t *testing.T) {
}

}

// test CheckHelmReleaseReady
func TestCheckHelmReleaseReady(t *testing.T) {
tests := []struct {
name string
helmRelease helmv2.HelmRelease
expected bool
}{
{
name: "helmrelease is ready",
helmRelease: helmv2.HelmRelease{
ObjectMeta: v1.ObjectMeta{
Name: "wego",
Namespace: "flux-system",
},
Status: helmv2.HelmReleaseStatus{
Conditions: []metav1.Condition{
{
Type: HelmReleaseReadyCondition,
Status: metav1.ConditionTrue,
},
},
},
},
expected: true,
},
{
name: "helmrelease is not ready",
helmRelease: helmv2.HelmRelease{
ObjectMeta: v1.ObjectMeta{
Name: "wego",
Namespace: "flux-system",
},
Status: helmv2.HelmReleaseStatus{
Conditions: []metav1.Condition{
{
Type: HelmReleaseReadyCondition,
Status: metav1.ConditionFalse,
},
},
},
},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := utils.CreateFakeClient(t, &tt.helmRelease)
ready, err := CheckHelmReleaseReady(client, "wego", "flux-system", 5*time.Second)

if err != nil {
assert.Equal(t, false, tt.expected, "error checking helmrelease: timeout waiting for HelmRelease wego to be ready")
return
}
assert.Equal(t, tt.expected, ready, "helmrelease is not ready")
})
}
}
95 changes: 0 additions & 95 deletions pkg/bootstrap/utils/status.go

This file was deleted.