-
Notifications
You must be signed in to change notification settings - Fork 33
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
Propagating Limitador's env vars #22
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,13 +21,14 @@ import ( | |
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
authorinov1beta1 "github.com/kuadrant/authorino-operator/api/v1beta1" | ||
limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1" | ||
istioapiv1alpha1 "istio.io/api/operator/v1alpha1" | ||
iopv1alpha1 "istio.io/istio/operator/pkg/apis/istio/v1alpha1" | ||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/core/v1" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
@@ -43,8 +44,14 @@ import ( | |
) | ||
|
||
const ( | ||
kuadrantFinalizer = "kuadrant.kuadrant.io/finalizer" | ||
extAuthorizerName = "kuadrant-authorization" | ||
kuadrantFinalizer = "kuadrant.kuadrant.io/finalizer" | ||
extAuthorizerName = "kuadrant-authorization" | ||
envLimitadorNamespace = "LIMITADOR_NAMESPACE" | ||
envLimitadorName = "LIMITADOR_NAME" | ||
) | ||
|
||
var ( | ||
limitadorName = common.FetchEnv(envLimitadorName, "limitador") | ||
) | ||
|
||
// KuadrantReconciler reconciles a Kuadrant object | ||
|
@@ -344,7 +351,7 @@ func (r *KuadrantReconciler) reconcileLimitador(ctx context.Context, kObj *kuadr | |
APIVersion: "limitador.kuadrant.io/v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "limitador", | ||
Name: limitadorName, | ||
Namespace: kObj.Namespace, | ||
}, | ||
Spec: limitadorv1alpha1.LimitadorSpec{}, | ||
|
@@ -389,14 +396,47 @@ func (r *KuadrantReconciler) createOnlyInKuadrantNSCb(ctx context.Context, kObj | |
return err | ||
} | ||
|
||
k8sObjKind := k8sObj.DeepCopyObject().GetObjectKind() | ||
var newObj client.Object | ||
newObj = k8sObj | ||
|
||
switch obj := k8sObj.(type) { | ||
case *appsv1.Deployment: // If it's a Deployment obj, it adds the required env vars | ||
obj.Spec.Template.Spec.Containers[0].Env = append( | ||
obj.Spec.Template.Spec.Containers[0].Env, | ||
v1.EnvVar{Name: envLimitadorNamespace, Value: kObj.Namespace}, | ||
v1.EnvVar{Name: envLimitadorName, Value: limitadorName}, | ||
) | ||
newObj = obj | ||
// TODO: DRY the following 2 case switches | ||
case *rbacv1.RoleBinding: | ||
if obj.Name == "kuadrant-leader-election-rolebinding" { | ||
for i, subject := range obj.Subjects { | ||
if subject.Name == "kuadrant-controller-manager" { | ||
obj.Subjects[i].Namespace = kObj.Namespace | ||
} | ||
} | ||
} | ||
newObj = obj | ||
case *rbacv1.ClusterRoleBinding: | ||
if obj.Name == "kuadrant-manager-rolebinding" { | ||
for i, subject := range obj.Subjects { | ||
if subject.Name == "kuadrant-controller-manager" { | ||
obj.Subjects[i].Namespace = kObj.Namespace | ||
} | ||
} | ||
} | ||
newObj = obj | ||
default: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This some code style we're following? |
||
} | ||
newObjCloned := newObj.DeepCopyObject() | ||
err = r.Client().Create(ctx, newObj) | ||
|
||
err = r.Client().Create(ctx, k8sObj) | ||
logger.V(1).Info("create resource", "GKV", k8sObjKind.GroupVersionKind(), "name", k8sObj.GetName(), "error", err) | ||
k8sObjKind := newObjCloned.GetObjectKind() | ||
logger.V(1).Info("create resource", "GKV", k8sObjKind.GroupVersionKind(), "name", newObj.GetName(), "error", err) | ||
if err != nil { | ||
if apierrors.IsAlreadyExists(err) { | ||
// Omit error | ||
logger.Info("Already exists", "GKV", k8sObjKind.GroupVersionKind(), "name", k8sObj.GetName()) | ||
logger.Info("Already exists", "GKV", k8sObjKind.GroupVersionKind(), "name", newObj.GetName()) | ||
} else { | ||
return err | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must be missing something here, but there is a lot of copying going on here.
Is the
switch obj := k8sObj.(type)
a copy itself? Trying to understand… but I wonder why not use thedefault:
if a "default" copy has to happen? That's if they are all different… I guess I need to refresh my golangThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, it's a bit misleading the
default
case, its block will be executed if none of the other cases match.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch obj := k8sObj.(type)
will only live within the switch block, so we need to copy it to a differentvar
.I'd love the swith case to include a
finally
clause, that will be executed at the very end, thus we could avoid a lot of repetition