Skip to content

Commit

Permalink
Support for Any Types in ExtraParams
Browse files Browse the repository at this point in the history
  • Loading branch information
zoetrope committed Jul 16, 2024
1 parent 73ada4c commit a880b4f
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 23 deletions.
42 changes: 42 additions & 0 deletions api/v1beta1/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package v1beta1

import (
"encoding/json"
)

// Params represents untyped configuration.
// +kubebuilder:validation:Type=object
type Params struct {
// Data holds the parameter keys and values.
Data map[string]interface{} `json:"-"`
}

// MarshalJSON implements the Marshaler interface.
func (c *Params) MarshalJSON() ([]byte, error) {
return json.Marshal(c.Data)
}

// UnmarshalJSON implements the Unmarshaler interface.
func (c *Params) UnmarshalJSON(data []byte) error {
var out map[string]interface{}
err := json.Unmarshal(data, &out)
if err != nil {
return err
}
c.Data = out
return nil
}

// DeepCopyInto is a deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (c *Params) DeepCopyInto(out *Params) {
bytes, err := json.Marshal(c.Data)
if err != nil {
panic(err)
}
var clone map[string]interface{}
err = json.Unmarshal(bytes, &clone)
if err != nil {
panic(err)
}
out.Data = clone
}
3 changes: 2 additions & 1 deletion api/v1beta1/tenant_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type TenantSpec struct {
ControllerName string `json:"controllerName,omitempty"`

// ExtraParams is a map of extra parameters that can be used in the templates.
// +kubebuilder:pruning:PreserveUnknownFields
// +optional
ExtraParams map[string]string `json:"extraParams,omitempty"`
ExtraParams Params `json:"extraParams,omitempty"`
}

// RootNamespaceSpec defines the desired state of Namespace.
Expand Down
18 changes: 11 additions & 7 deletions api/v1beta1/zz_generated.deepcopy.go

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

3 changes: 1 addition & 2 deletions charts/cattage/crds/tenant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ spec:
type: object
type: array
extraParams:
additionalProperties:
type: string
description: ExtraParams is a map of extra parameters that can be used in the templates.
type: object
x-kubernetes-preserve-unknown-fields: true
rootNamespaces:
description: RootNamespaces are the list of root namespaces that belong to this tenant.
items:
Expand Down
3 changes: 1 addition & 2 deletions config/crd/bases/cattage.cybozu.io_tenants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@ spec:
type: object
type: array
extraParams:
additionalProperties:
type: string
description: ExtraParams is a map of extra parameters that can be
used in the templates.
type: object
x-kubernetes-preserve-unknown-fields: true
rootNamespaces:
description: RootNamespaces are the list of root namespaces that belong
to this tenant.
Expand Down
4 changes: 4 additions & 0 deletions config/manager/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ data:
- namespace: {{ . }}
server: '*'
{{- end }}
{{- range .ExtraParams.Destinations }}
- namespace: {{ . }}
server: '*'
{{- end }}
namespaceResourceBlacklist:
- group: ""
kind: ResourceQuota
Expand Down
4 changes: 3 additions & 1 deletion config/samples/tenant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ spec:
roles:
- admin
extraParams:
GitHubTeam: b-team-gh
Destinations:
- "extra-namespace-x"
- "extra-namespace-y"
12 changes: 6 additions & 6 deletions controllers/tenant_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (r *TenantReconciler) rolesMap(ctx context.Context, delegates []cattagev1be
}
result[role] = append(result[role], Role{
Name: delegatedTenant.Name,
ExtraParams: delegatedTenant.Spec.ExtraParams,
ExtraParams: delegatedTenant.Spec.ExtraParams.Data,
})
}
}
Expand Down Expand Up @@ -417,11 +417,11 @@ func (r *TenantReconciler) reconcileNamespaces(ctx context.Context, tenant *catt
err = tpl.Execute(&buf, struct {
Name string
Roles map[string][]Role
ExtraParams map[string]string
ExtraParams map[string]interface{}
}{
Name: tenant.Name,
Roles: roles,
ExtraParams: tenant.Spec.ExtraParams,
ExtraParams: tenant.Spec.ExtraParams.Data,
})
if err != nil {
return err
Expand Down Expand Up @@ -467,7 +467,7 @@ func (r *TenantReconciler) reconcileNamespaces(ctx context.Context, tenant *catt

type Role struct {
Name string
ExtraParams map[string]string
ExtraParams map[string]interface{}
}

func (r *TenantReconciler) reconcileArgoCD(ctx context.Context, tenant *cattagev1beta1.Tenant) error {
Expand Down Expand Up @@ -505,13 +505,13 @@ func (r *TenantReconciler) reconcileArgoCD(ctx context.Context, tenant *cattagev
Namespaces []string
Roles map[string][]Role
Repositories []string
ExtraParams map[string]string
ExtraParams map[string]interface{}
}{
Name: tenant.Name,
Namespaces: namespaces,
Roles: roles,
Repositories: tenant.Spec.ArgoCD.Repositories,
ExtraParams: tenant.Spec.ExtraParams,
ExtraParams: tenant.Spec.ExtraParams.Data,
})
if err != nil {
return err
Expand Down
20 changes: 16 additions & 4 deletions controllers/tenant_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ var _ = Describe("Tenant controller", Ordered, func() {
"https://github.com/cybozu-go/*",
},
},
ExtraParams: map[string]string{
ExtraParams: cattagev1beta1.Params{Data: map[string]interface{}{
"GitHubTeam": "c-team-gh",
},
}},
},
}
err := k8sClient.Create(ctx, cTeam)
Expand Down Expand Up @@ -143,9 +143,13 @@ var _ = Describe("Tenant controller", Ordered, func() {
},
},
},
ExtraParams: map[string]string{
ExtraParams: cattagev1beta1.Params{Data: map[string]interface{}{
"GitHubTeam": "x-team-gh",
},
"Destinations": []string{
"extra-namespace-x",
"extra-namespace-y",
},
}},
},
}
err = k8sClient.Create(ctx, xTeam)
Expand Down Expand Up @@ -209,6 +213,14 @@ var _ = Describe("Tenant controller", Ordered, func() {
"namespace": Equal("sub-4"),
"server": Equal("*"),
}),
MatchAllKeys(Keys{
"namespace": Equal("extra-namespace-x"),
"server": Equal("*"),
}),
MatchAllKeys(Keys{
"namespace": Equal("extra-namespace-y"),
"server": Equal("*"),
}),
),
"namespaceResourceBlacklist": ConsistOf(
MatchAllKeys(Keys{
Expand Down

0 comments on commit a880b4f

Please sign in to comment.