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

fix: move awsacfg resource creation from Helm chart to controller #61

Merged
merged 18 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
check if resource exists using Get and use ctrl.New instead of mgr.Ge…
…tClient
  • Loading branch information
kbeniwal committed Mar 7, 2023
commit 150f3915c879d2869b474e44f6a4251af6b71c7c
30 changes: 22 additions & 8 deletions controllers/awsadapterconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
apimachineryTypes "k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -446,17 +447,34 @@ func (r *AWSAdapterConfigReconciler) updateLastPollStatusFailure(ctx context.Con
return ctrl.Result{RequeueAfter: r.RequeueInterval}, nil
}

func (r *AWSAdapterConfigReconciler) CreateCRIfNotPresent() {
func (r *AWSAdapterConfigReconciler) IsCRPresent() bool {
l := log.FromContext(context.TODO())

obj := &securityv1alpha1.AWSAdapterConfig{}
err := r.Get(context.TODO(), apimachineryTypes.NamespacedName{Namespace: getAdapterNamespace(), Name: getAdapterName()}, obj)
if err != nil {
if !errors.IsNotFound(err) {
l.Error(err, "Error checking if AWS SDK config exists")
}

return false
}
return true
}

func (r *AWSAdapterConfigReconciler) CreateCR() {
l := log.FromContext(context.TODO())

l.Info("Creating AWS SDK config")

clusterName := getClusterName()
clusterRegion := getClusterRegion()
adapterName := getAdapterName()
adapterNamespace := getAdapterNamespace()
res := &securityv1alpha1.AWSAdapterConfig{
ObjectMeta: metav1.ObjectMeta{
Name: getAdapterName(),
Namespace: getAdapterNamespace(),
Name: adapterName,
Namespace: adapterNamespace,
},
Spec: securityv1alpha1.AWSAdapterConfigSpec{
Name: &clusterName,
Expand All @@ -468,11 +486,7 @@ func (r *AWSAdapterConfigReconciler) CreateCRIfNotPresent() {
if err == nil {
l.Info("AWS SDK config created successfully")
} else {
if errors.IsAlreadyExists(err) {
l.Info("AWS SDK config already exists. Skipping resource creation.")
} else {
l.Error(err, "Error creating AWS SDK config")
}
l.Error(err, "Error creating AWS SDK config")
}
}

Expand Down
19 changes: 15 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

Expand Down Expand Up @@ -92,12 +93,18 @@ func main() {
os.Exit(1)
}

reconciler := &controllers.AWSAdapterConfigReconciler{
Client: mgr.GetClient(),
cl, err := client.New(ctrl.GetConfigOrDie(), client.Options{Scheme: scheme})
if err != nil {
setupLog.Error(err, "unable to create client")
os.Exit(1)
}

r := &controllers.AWSAdapterConfigReconciler{
Client: cl,
pns-nirmata marked this conversation as resolved.
Show resolved Hide resolved
Scheme: mgr.GetScheme(),
RequeueInterval: time.Duration(syncPeriod) * time.Minute,
}
if err = reconciler.SetupWithManager(mgr); err != nil {
if err = r.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSAdapterConfig")
os.Exit(1)
}
Expand All @@ -112,7 +119,11 @@ func main() {
os.Exit(1)
}

reconciler.CreateCRIfNotPresent()
if !r.IsCRPresent() {
r.CreateCR()
} else {
ctrl.Log.Info("AWS SDK config already exists. Skipping resource creation.")
}

pns-nirmata marked this conversation as resolved.
Show resolved Hide resolved
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
Expand Down