Skip to content

Commit

Permalink
operator: Introduce NamespaceNameRef type which is used instead of Ob…
Browse files Browse the repository at this point in the history
…jectReference

- Rename ClusterKeyRef to ClusterRef
- Use the NamespaceNameRef for ClusterRef, Google SSO ClientCredentialsRef
- REF https://pkg.go.dev/k8s.io/api/core/v1#ObjectReference
  • Loading branch information
pvsune committed Sep 8, 2022
1 parent eaf0a9a commit 3860214
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 44 deletions.
12 changes: 12 additions & 0 deletions src/go/k8s/apis/redpanda/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,15 @@ func (s *SecretKeyRef) GetValue(secret *corev1.Secret, defaultKey string) ([]byt
}
return value, nil
}

// NamespaceNameRef contains namespace and name to inspect or modify the referred object
// REF https://pkg.go.dev/k8s.io/api/core/v1#ObjectReference
type NamespaceNameRef struct {
// Name of the referent.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
Name string `json:"name"`

// Namespace of the referent.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
Namespace string `json:"namespace"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type EnterpriseLoginGoogle struct {

// ClientCredentials is the Secret that contains SSO credentials
// The Secret should contain keys "clientId", "clientSecret"
ClientCredentialsRef SecretKeyRef `json:"clientCredentialsRef"`
ClientCredentialsRef NamespaceNameRef `json:"clientCredentialsRef"`

// Use Google groups in your RBAC role bindings.
Directory *EnterpriseLoginGoogleDirectory `json:"directory,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions src/go/k8s/apis/redpanda/v1alpha1/console_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ConsoleSpec struct {
SchemaRegistry Schema `json:"schema"`

// The referenced Redpanda Cluster
ClusterKeyRef corev1.ObjectReference `json:"clusterKeyRef"`
ClusterRef NamespaceNameRef `json:"clusterRef"`

Deployment Deployment `json:"deployment"`
Connect Connect `json:"connect"`
Expand Down Expand Up @@ -216,12 +216,12 @@ var AllowConsoleAnyNamespace bool

// IsAllowedNamespace returns true if Console is valid to be created in current namespace
func (c *Console) IsAllowedNamespace() bool {
return AllowConsoleAnyNamespace || c.GetNamespace() == c.Spec.ClusterKeyRef.Namespace
return AllowConsoleAnyNamespace || c.GetNamespace() == c.Spec.ClusterRef.Namespace
}

// GetClusterRef returns the NamespacedName of referenced Cluster object
func (c *Console) GetClusterRef() types.NamespacedName {
return types.NamespacedName{Name: c.Spec.ClusterKeyRef.Name, Namespace: c.Spec.ClusterKeyRef.Namespace}
return types.NamespacedName{Name: c.Spec.ClusterRef.Name, Namespace: c.Spec.ClusterRef.Namespace}
}

//+kubebuilder:object:root=true
Expand Down
17 changes: 16 additions & 1 deletion src/go/k8s/apis/redpanda/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 5 additions & 31 deletions src/go/k8s/config/crd/bases/redpanda.vectorized.io_consoles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,18 @@ spec:
description: ConsoleSpec defines the desired state of Console Most of
the fields here are copied from Console config REF https://github.com/redpanda-data/console/blob/master/backend/pkg/api/config.go
properties:
clusterKeyRef:
clusterRef:
description: The referenced Redpanda Cluster
properties:
apiVersion:
description: API version of the referent.
type: string
fieldPath:
description: 'If referring to a piece of an object instead of
an entire object, this string should contain a valid JSON/Go
field access statement, such as desiredState.manifest.containers[2].
For example, if the object reference is to a container within
a pod, this would take on a value like: "spec.containers{name}"
(where "name" refers to the name of the container that triggered
the event) or if no container name is specified "spec.containers[2]"
(container with index 2 in this pod). This syntax is chosen
only to have some well-defined way of referencing a part of
an object. TODO: this design is not final and this field is
subject to change in the future.'
type: string
kind:
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
type: string
namespace:
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
type: string
resourceVersion:
description: 'Specific resourceVersion to which this reference
is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
type: string
uid:
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
type: string
required:
- name
- namespace
type: object
connect:
description: Connect defines configurable fields for Kafka Connect
Expand Down Expand Up @@ -317,9 +294,6 @@ spec:
SSO credentials The Secret should contain keys "clientId",
"clientSecret"
properties:
key:
description: Key in Secret data to get value from
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
type: string
Expand Down Expand Up @@ -460,7 +434,7 @@ spec:
type: string
type: object
required:
- clusterKeyRef
- clusterRef
- connect
- deployment
- schema
Expand Down
2 changes: 1 addition & 1 deletion src/go/k8s/config/samples/console.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spec:
listenPort: 8080
schema:
enabled: true
clusterKeyRef:
clusterRef:
name: cluster
namespace: default
deployment:
Expand Down
2 changes: 1 addition & 1 deletion src/go/k8s/controllers/redpanda/console_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (r *ConsoleReconciler) Reconcile(
console,
corev1.EventTypeWarning, ClusterNotFoundEvent,
"Unable to reconcile Console as the referenced Cluster %s/%s is not found",
console.Spec.ClusterKeyRef.Namespace, console.Spec.ClusterKeyRef.Name,
console.Spec.ClusterRef.Namespace, console.Spec.ClusterRef.Name,
)
}
return ctrl.Result{}, err
Expand Down
4 changes: 2 additions & 2 deletions src/go/k8s/controllers/redpanda/console_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var _ = Describe("Console controller", func() {
Namespace: ConsoleNamespace,
},
Spec: redpandav1alpha1.ConsoleSpec{
ClusterKeyRef: corev1.ObjectReference{Namespace: key.Namespace, Name: key.Name},
ClusterRef: redpandav1alpha1.NamespaceNameRef{Namespace: key.Namespace, Name: key.Name},
SchemaRegistry: redpandav1alpha1.Schema{Enabled: enableSchemaRegistry},
Deployment: redpandav1alpha1.Deployment{Image: deploymentImage},
Connect: redpandav1alpha1.Connect{Enabled: enableConnect},
Expand Down Expand Up @@ -308,7 +308,7 @@ var _ = Describe("Console controller", func() {
},
Google: &redpandav1alpha1.EnterpriseLoginGoogle{
Enabled: true,
ClientCredentialsRef: redpandav1alpha1.SecretKeyRef{
ClientCredentialsRef: redpandav1alpha1.NamespaceNameRef{
Name: googleName,
Namespace: ConsoleNamespace,
},
Expand Down
5 changes: 4 additions & 1 deletion src/go/k8s/pkg/console/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ func (cm *ConfigMap) genLogin(ctx context.Context) (e EnterpriseLogin, err error

switch { // nolint:gocritic // will support more providers
case provider.Google != nil:
cc := provider.Google.ClientCredentialsRef
cc := redpandav1alpha1.SecretKeyRef{
Namespace: provider.Google.ClientCredentialsRef.Namespace,
Name: provider.Google.ClientCredentialsRef.Name,
}
ccSecret, err := cc.GetSecret(ctx, cm.Client)
if err != nil {
return e, err
Expand Down
2 changes: 1 addition & 1 deletion src/go/k8s/tests/e2e/console/01-console.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:
listenPort: 8080
schema:
enabled: true
clusterKeyRef:
clusterRef:
name: cluster
namespace: default
deployment:
Expand Down
4 changes: 2 additions & 2 deletions src/go/k8s/webhooks/redpanda/console_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func (v *ConsoleValidator) Handle(
}

if !console.IsAllowedNamespace() {
return admission.Denied(fmt.Sprintf("cluster %s/%s is in different namespace", console.Spec.ClusterKeyRef.Namespace, console.Spec.ClusterKeyRef.Name))
return admission.Denied(fmt.Sprintf("cluster %s/%s is in different namespace", console.Spec.ClusterRef.Namespace, console.Spec.ClusterRef.Name))
}

cluster := &redpandav1alpha1.Cluster{}
if err := v.Client.Get(ctx, console.GetClusterRef(), cluster); err != nil {
if apierrors.IsNotFound(err) {
return admission.Denied(fmt.Sprintf("cluster %s/%s not found", console.Spec.ClusterKeyRef.Namespace, console.Spec.ClusterKeyRef.Name))
return admission.Denied(fmt.Sprintf("cluster %s/%s not found", console.Spec.ClusterRef.Namespace, console.Spec.ClusterRef.Name))
}
return admission.Errored(http.StatusBadRequest, err)
}
Expand Down

0 comments on commit 3860214

Please sign in to comment.