Skip to content

feat: Combine generic variables with provider specific variables #173

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

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
21 changes: 16 additions & 5 deletions api/v1alpha1/aws_clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package v1alpha1

import (
"maps"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

Expand All @@ -24,17 +26,26 @@ type AWSClusterConfig struct {
type AWSClusterConfigSpec struct {
// +optional
Region *Region `json:"region,omitempty"`

GenericClusterConfig `json:",inline"`
}

func (AWSClusterConfigSpec) VariableSchema() clusterv1.VariableSchema {
clusterConfigProps := GenericClusterConfig{}.VariableSchema().OpenAPIV3Schema.Properties

maps.Copy(
clusterConfigProps,
map[string]clusterv1.JSONSchemaProps{
"region": Region("").VariableSchema().OpenAPIV3Schema,
},
)

return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Description: "AWS Cluster configuration",
Description: "AWS cluster configuration",
Type: "object",
Properties: map[string]clusterv1.JSONSchemaProps{
"region": Region("").VariableSchema().OpenAPIV3Schema,
},
Required: []string{"region"},
Properties: clusterConfigProps,
Required: []string{"region"},
},
}
}
Expand Down
22 changes: 3 additions & 19 deletions api/v1alpha1/clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
Expand All @@ -15,18 +14,8 @@ const (
CNIProviderCalico = "calico"
)

//+kubebuilder:object:root=true

// ClusterConfig is the Schema for the clusterconfigs API.
type ClusterConfig struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ClusterConfigSpec `json:"spec,omitempty"`
}

// ClusterConfigSpec defines the desired state of ClusterConfig.
type ClusterConfigSpec struct {
// GenericClusterConfig defines the generic cluster configdesired.
type GenericClusterConfig struct {
// +optional
KubernetesImageRepository *KubernetesImageRepository `json:"kubernetesImageRepository,omitempty"`

Expand All @@ -43,7 +32,7 @@ type ClusterConfigSpec struct {
Addons *Addons `json:"addons,omitempty"`
}

func (ClusterConfigSpec) VariableSchema() clusterv1.VariableSchema {
func (GenericClusterConfig) VariableSchema() clusterv1.VariableSchema {
return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Description: "Cluster configuration",
Expand Down Expand Up @@ -240,8 +229,3 @@ func (NFD) VariableSchema() clusterv1.VariableSchema {
},
}
}

// +kubebuilder:object:root=true
func init() {
SchemeBuilder.Register(&ClusterConfig{})
}
40 changes: 40 additions & 0 deletions api/v1alpha1/docker_clusterconfig_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

//+kubebuilder:object:root=true

// DockerClusterConfig is the Schema for the dockerclusterconfigs API.
type DockerClusterConfig struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec AWSClusterConfigSpec `json:"spec,omitempty"`
}

// DockerClusterConfigSpec defines the desired state of DockerClusterConfig.
type DockerClusterConfigSpec struct {
GenericClusterConfig `json:",inline"`
}

func (DockerClusterConfigSpec) VariableSchema() clusterv1.VariableSchema {
clusterConfigProps := GenericClusterConfig{}.VariableSchema().OpenAPIV3Schema.Properties

return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Description: "Docker cluster configuration",
Type: "object",
Properties: clusterConfigProps,
},
}
}

func init() {
SchemeBuilder.Register(&DockerClusterConfig{})
}
85 changes: 51 additions & 34 deletions api/v1alpha1/zz_generated.deepcopy.go

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

65 changes: 27 additions & 38 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/server"
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation/region"
genericclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
dockerclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle/cni/calico"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle/nfd"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle/servicelbgc"
Expand Down Expand Up @@ -119,60 +119,49 @@ func main() {
}

// Handlers for lifecycle hooks.
genericLifeCycleHandlers := []handlers.Named{
genericLifecycleHandlers := []handlers.Named{
calico.New(mgr.GetClient(), calicoCNIConfig),
nfd.New(mgr.GetClient(), nfdConfig),
servicelbgc.New(mgr.GetClient()),
}
// Handlers that apply patches to the Cluster object and its objects.
// Used by CAPI's GeneratePatches hook.
genericPatchHandlers := []handlers.Named{
httpproxy.NewPatch(mgr.GetClient()),
extraapiservercertsans.NewPatch(),
auditpolicy.NewPatch(),
kubernetesimagerepository.NewPatch(),
etcd.NewPatch(),
}
// Handlers used by CAPI's DiscoverVariables hook.
// It's ok that this does not match patchHandlers.
// Some of those handlers may always get applied and not have a corresponding variable.
genericVariableHandlers := []handlers.Named{
httpproxy.NewVariable(),
extraapiservercertsans.NewVariable(),
kubernetesimagerepository.NewVariable(),
}

// This genericMetaPatchHandlers combines all other patch and variable handlers under a single handler.
// It allows to specify configuration under a single variable.
genericMetaPatchHandlers := []mutation.MetaMutater{
httpproxy.NewMetaPatch(mgr.GetClient()),
extraapiservercertsans.NewMetaPatch(),
auditpolicy.NewPatch(),
kubernetesimagerepository.NewMetaPatch(),
etcd.NewMetaPatch(),
}
genericMetaHandlers := []handlers.Named{
// This Calico handler relies on a variable but does not generate a patch.
// Instead it creates other resources in the API.
calico.NewMetaHandler(mgr.GetClient(), calicoCNIConfig),
nfd.NewMetaHandler(mgr.GetClient(), nfdConfig),
genericclusterconfig.NewVariable(),
mutation.NewMetaGeneratePatchesHandler("clusterConfigPatch", genericMetaPatchHandlers...),
extraapiservercertsans.NewMetaPatch(),
httpproxy.NewMetaPatch(mgr.GetClient()),
kubernetesimagerepository.NewMetaPatch(),
}

// This awsMetaPatchHandlers combines all AWS patch and variable handlers under a single handler.
// awsMetaPatchHandlers combines all AWS patch and variable handlers under a single handler.
// It allows to specify configuration under a single variable.
awsMetaPatchHandlers := []mutation.MetaMutater{
region.NewMetaPatch(),
}
awsMetaPatchHandlers := append(
[]mutation.MetaMutater{
region.NewMetaPatch(),
},
genericMetaPatchHandlers...,
)
awsMetaHandlers := []handlers.Named{
awsclusterconfig.NewVariable(),
mutation.NewMetaGeneratePatchesHandler("awsClusterConfigPatch", awsMetaPatchHandlers...),
}

// dockerMetaPatchHandlers combines all Docker patch and variable handlers under a single handler.
// It allows to specify configuration under a single variable.
dockerMetaPatchHandlers := []mutation.MetaMutater{}
dockerMetaHandlers := []handlers.Named{
dockerclusterconfig.NewVariable(),
mutation.NewMetaGeneratePatchesHandler(
"dockerClusterConfigPatch",
dockerMetaPatchHandlers...),
}

var allHandlers []handlers.Named
allHandlers = append(allHandlers, genericLifeCycleHandlers...)
allHandlers = append(allHandlers, genericPatchHandlers...)
allHandlers = append(allHandlers, genericVariableHandlers...)
allHandlers = append(allHandlers, genericMetaHandlers...)
allHandlers = append(allHandlers, genericLifecycleHandlers...)
allHandlers = append(allHandlers, awsMetaHandlers...)
allHandlers = append(allHandlers, dockerMetaHandlers...)

runtimeWebhookServer := server.NewServer(runtimeWebhookServerOpts, allHandlers...)

Expand Down
8 changes: 4 additions & 4 deletions docs/content/audit-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ title: "Audit policy"

Kubernetes auditing provides a security-relevant, chronological set of records documenting the sequence of actions in a
cluster. The cluster audits the activities generated by users, by applications that use the Kubernetes API, and by the
control plane itself. The `auditpolicypatch` external patch will generate appropriate configuration for the Kubernetes
control plane itself. The `clusterconfigpatch` external patch will generate appropriate configuration for the Kubernetes
control plane.

To enable the audit policy enable the `auditpolicypatch` external patch on `ClusterClass`.
To enable the meta handler enable the `clusterconfigvars` and `clusterconfigpatch` external patches on `ClusterClass`.

```yaml
apiVersion: cluster.x-k8s.io/v1beta1
Expand All @@ -16,9 +16,9 @@ metadata:
name: <NAME>
spec:
patches:
- name: audit-policy
- name: cluster-config
external:
generateExtension: "auditpolicypatch.capi-runtime-extensions"
generateExtension: "clusterconfigpatch.capi-runtime-extensions"
```

Applying this configuration will result in new bootstrap files on the `KubeadmControlPlaneTemplate`.
3 changes: 1 addition & 2 deletions docs/content/calico-cni.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ The hook creates two `ClusterResourceSets`: one to deploy the Tigera Operator, a
Calico via the Tigera `Installation` CRD. The Tigera Operator CRS is shared between all clusters in the operator,
whereas the Calico installation CRS is unique per cluster.

To enable the meta handler enable the `clusterconfigvars` and `clusterconfigpatch`
external patches on `ClusterClass`.
To enable the meta handler enable the `clusterconfigvars` and `clusterconfigpatch` external patches on `ClusterClass`.

```yaml
apiVersion: cluster.x-k8s.io/v1beta1
Expand Down
Loading