Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
loganmc10 committed Jul 5, 2023
1 parent 630370c commit 5e0ffa8
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions internal/acm/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
agentv1 "github.com/stolostron/klusterlet-addon-controller/pkg/apis/agent/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
Expand All @@ -33,18 +34,36 @@ import (
//+kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings,verbs=create
//+kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=create

// returns nil if the Klusterlet is Available, error otherwise
func checkKlusterlet(ctx context.Context, c client.Client, logger logr.Logger) error {
klusterlet := &operatorapiv1.Klusterlet{}
err := c.Get(ctx, types.NamespacedName{Name: "klusterlet"}, klusterlet)
if err == nil {
klusterletCondition := apimeta.FindStatusCondition(klusterlet.Status.Conditions, "Available")
if klusterletCondition.Status == metav1.ConditionTrue {
logger.Info("cluster registered to ACM")
} else {
return fmt.Errorf("cluster not registered to ACM")
}
}
return err
}

func Reconcile(ctx context.Context, c client.Client, scheme *runtime.Scheme, relocation *rhsysenggithubiov1beta1.ClusterRelocation, logger logr.Logger) error {
if relocation.Spec.ACMRegistration == nil {
return nil
}

klusterlet := &operatorapiv1.Klusterlet{}
if err := c.Get(ctx, types.NamespacedName{Name: "klusterlet"}, klusterlet); err == nil {
// TODO: ensure klusterlet status is good
logger.Info("cluster already registered to ACM")
// skip these steps if the cluster is already registered to ACM
if checkKlusterlet(ctx, c, logger) == nil {
return nil
}

// the tokenSecret holds the credentials for the ACM cluster
// these credentials should allow the following:
// Creating ManagedClusters (these are cluster scoped resources)
// Creating KlusterletAddonConfigs (these are namespace scoped resources)
// Getting Secrets (these are namespace scoped resources)
tokenSecret := &corev1.Secret{}
if err := c.Get(ctx, types.NamespacedName{Name: relocation.Spec.ACMRegistration.TokenRef.Name, Namespace: relocation.Spec.ACMRegistration.TokenRef.Namespace}, tokenSecret); err != nil {
return err
Expand Down Expand Up @@ -88,6 +107,7 @@ func Reconcile(ctx context.Context, c client.Client, scheme *runtime.Scheme, rel
break
}

// the import secret that we obtained from the ACM cluster contains YAML manifests that need to be applied here
d := yaml.NewYAMLToJSONDecoder(bytes.NewReader(importSecret.Data["crds.yaml"]))
for {
klusterletCRDObj := &unstructured.Unstructured{}
Expand All @@ -109,6 +129,7 @@ func Reconcile(ctx context.Context, c client.Client, scheme *runtime.Scheme, rel
}
}

// the import secret that we obtained from the ACM cluster contains YAML manifests that need to be applied here
d = yaml.NewYAMLToJSONDecoder(bytes.NewReader(importSecret.Data["import.yaml"]))
for {
importObj := &unstructured.Unstructured{}
Expand Down Expand Up @@ -145,7 +166,17 @@ func Reconcile(ctx context.Context, c client.Client, scheme *runtime.Scheme, rel
}
}

// TODO: wait for klusterlet to finish deploying
// wait for the Klusterlet to become Available
startTime := time.Now()
for {
if checkKlusterlet(ctx, c, logger) == nil {
return nil
}
time.Sleep(time.Second * 10)

return nil
// we set a 5 minute timeout in case the Klusterlet never gets to Available
if time.Since(startTime) > time.Minute*5 {
return fmt.Errorf("klusterlet error")
}
}
}

0 comments on commit 5e0ffa8

Please sign in to comment.