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 99a6efe
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 21 deletions.
49 changes: 49 additions & 0 deletions api/v1beta1/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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:"-"`
}

func (c *Params) ToMap() map[string]interface{} {
if c == nil {
return nil
}
return c.Data
}

// 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
15 changes: 11 additions & 4 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.ToMap(),
})
}
}
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.ToMap(),
})
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.ToMap(),
})
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
4 changes: 4 additions & 0 deletions controllers/testdata/appprojecttemplate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ spec:
- namespace: {{ . }}
server: '*'
{{- end }}
{{- range .ExtraParams.Destinations }}
- namespace: {{ . }}
server: '*'
{{- end }}
namespaceResourceBlacklist:
- group: ""
kind: ResourceQuota
Expand Down
2 changes: 1 addition & 1 deletion docs/crd_tenant.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ TenantSpec defines the desired state of Tenant.
| argocd | ArgoCD is the settings of Argo CD for this tenant. | [ArgoCDSpec](#argocdspec) | false |
| delegates | Delegates is a list of other tenants that are delegated access to this tenant. | [][DelegateSpec](#delegatespec) | false |
| controllerName | ControllerName is the name of the application-controller that manages this tenant's applications. If not specified, the default controller is used. | string | false |
| extraParams | ExtraParams is a map of extra parameters that can be used in the templates. | map[string]string | false |
| extraParams | ExtraParams is a map of extra parameters that can be used in the templates. | Params | false |

[Back to Custom Resources](#custom-resources)

Expand Down

0 comments on commit 99a6efe

Please sign in to comment.