Skip to content

Commit 54ba491

Browse files
authored
feat: Add ClusterConfig variable and patch handler (#142)
1 parent abb8651 commit 54ba491

26 files changed

+788
-110
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ public/
3636
resources/
3737
node_modules/
3838
.hugo_build.lock
39+
40+
/cluster.yaml

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,7 @@ issues:
8181
- source: "^// \\+kubebuilder:"
8282
linters:
8383
- lll
84+
# Idiomatic to use init functions to register APIs with scheme
85+
- path: "api/*"
86+
linters:
87+
- gochecknoinits

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ gomod:
2828

2929
builds:
3030
- id: capi-runtime-extensions
31-
dir: ./cmd/capi-runtime-extensions
31+
dir: ./cmd
3232
env:
3333
- CGO_ENABLED=0
3434
flags:

PROJECT

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ layout:
77
- go.kubebuilder.io/v4
88
projectName: capi-runtime-extensions
99
repo: github.com/d2iq-labs/capi-runtime-extensions
10+
resources:
11+
- api:
12+
crdVersion: v1
13+
namespaced: true
14+
domain: labs.d2iq.io
15+
group: capiext.labs.d2iq.io
16+
kind: ClusterConfig
17+
path: github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1
18+
version: v1alpha1
1019
version: "3"

api/v1alpha1/clusterconfig_types.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package v1alpha1
5+
6+
import (
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
9+
10+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/openapi/patterns"
11+
)
12+
13+
//+kubebuilder:object:root=true
14+
15+
// ClusterConfig is the Schema for the clusterconfigs API.
16+
type ClusterConfig struct {
17+
metav1.TypeMeta `json:",inline"`
18+
metav1.ObjectMeta `json:"metadata,omitempty"`
19+
20+
Spec ClusterConfigSpec `json:"spec,omitempty"`
21+
}
22+
23+
// ClusterConfigSpec defines the desired state of ClusterConfig.
24+
type ClusterConfigSpec struct {
25+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
26+
// Important: Run "make" to regenerate code after modifying this file
27+
28+
// +optional
29+
Proxy *HTTPProxy `json:"proxy,omitempty"`
30+
31+
// +optional
32+
ExtraAPIServerCertSANs ExtraAPIServerCertSANs `json:"extraAPIServerCertSANs,omitempty"`
33+
}
34+
35+
func (ClusterConfigSpec) VariableSchema() clusterv1.VariableSchema {
36+
return clusterv1.VariableSchema{
37+
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
38+
Description: "Cluster configuration",
39+
Type: "object",
40+
Properties: map[string]clusterv1.JSONSchemaProps{
41+
"proxy": HTTPProxy{}.VariableSchema().OpenAPIV3Schema,
42+
"extraAPIServerCertSANs": ExtraAPIServerCertSANs{}.VariableSchema().OpenAPIV3Schema,
43+
},
44+
},
45+
}
46+
}
47+
48+
// HTTPProxy required for providing proxy configuration.
49+
type HTTPProxy struct {
50+
// HTTP proxy.
51+
HTTP string `json:"http,omitempty"`
52+
53+
// HTTPS proxy.
54+
HTTPS string `json:"https,omitempty"`
55+
56+
// AdditionalNo Proxy list that will be added to the automatically calculated
57+
// values that will apply no_proxy configuration for cluster internal network.
58+
// Default values: localhost,127.0.0.1,<POD_NETWORK>,<SERVICE_NETWORK>,kubernetes
59+
// ,kubernetes.default,.svc,.svc.<SERVICE_DOMAIN>
60+
AdditionalNo []string `json:"additionalNo"`
61+
}
62+
63+
func (HTTPProxy) VariableSchema() clusterv1.VariableSchema {
64+
return clusterv1.VariableSchema{
65+
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
66+
Type: "object",
67+
Properties: map[string]clusterv1.JSONSchemaProps{
68+
"http": {
69+
Description: "HTTP proxy value.",
70+
Type: "string",
71+
},
72+
"https": {
73+
Description: "HTTPS proxy value.",
74+
Type: "string",
75+
},
76+
"additionalNo": {
77+
Description: "Additional No Proxy list that will be added to the automatically calculated " +
78+
"values required for cluster internal network. " +
79+
"Default value: localhost,127.0.0.1,<POD_NETWORK>,<SERVICE_NETWORK>,kubernetes," +
80+
"kubernetes.default,.svc,.svc.<SERVICE_DOMAIN>",
81+
Type: "array",
82+
Items: &clusterv1.JSONSchemaProps{
83+
Type: "string",
84+
},
85+
},
86+
},
87+
},
88+
}
89+
}
90+
91+
// ExtraAPIServerCertSANs required for providing API server cert SANs.
92+
type ExtraAPIServerCertSANs []string
93+
94+
func (ExtraAPIServerCertSANs) VariableSchema() clusterv1.VariableSchema {
95+
return clusterv1.VariableSchema{
96+
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
97+
Description: "Extra Subject Alternative Names for the API Server signing cert",
98+
Type: "array",
99+
UniqueItems: true,
100+
Items: &clusterv1.JSONSchemaProps{
101+
Type: "string",
102+
Pattern: patterns.Anchored(patterns.DNS1123Subdomain),
103+
},
104+
},
105+
}
106+
}
107+
108+
// +kubebuilder:object:root=true
109+
func init() {
110+
SchemeBuilder.Register(&ClusterConfig{})
111+
}

api/v1alpha1/doc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package v1alpha1 contains API Schema definitions for the CAPI extensions v1alpha1 API group
5+
// +kubebuilder:object:generate=true
6+
// +groupName=capiext.labs.d2iq.io
7+
//
8+
//go:generate -command CTRLGEN controller-gen paths="./..."
9+
//go:generate CTRLGEN rbac:headerFile="../../hack/license-header.yaml.txt",roleName=capi-runtime-extensions-manager-role output:rbac:artifacts:config=../../charts/capi-runtime-extensions/templates
10+
//go:generate CTRLGEN object:headerFile="../../hack/license-header.go.txt" output:object:artifacts:config=/dev/null
11+
package v1alpha1

api/v1alpha1/groupversion_info.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
// Copyright 2023 D2iQ, Inc. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
// Package v1alpha1 contains API Schema definitions for the CAPI extensions v1alpha1 API group
5-
// +kubebuilder:object:generate=true
6-
// +groupName=capiext.labs.d2iq.io
74
package v1alpha1
85

96
import (
107
"k8s.io/apimachinery/pkg/runtime/schema"
118
"sigs.k8s.io/controller-runtime/pkg/scheme"
129
)
1310

11+
const APIGroup = "capiext.labs.d2iq.io"
12+
1413
var (
1514
// GroupVersion is group version used to register these objects.
16-
GroupVersion = schema.GroupVersion{Group: "capiext.labs.d2iq.io", Version: "v1alpha1"}
15+
GroupVersion = schema.GroupVersion{Group: APIGroup, Version: "v1alpha1"}
1716

1817
// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
1918
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 93 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/capi-runtime-extensions/main.go renamed to cmd/main.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/manager"
2727
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
2828

29+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
2930
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/server"
31+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/auditpolicy"
32+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/clusterconfig"
3033
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/cni/calico"
3134
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/extraapiservercertsans"
3235
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/httpproxy"
@@ -130,12 +133,29 @@ func main() {
130133

131134
runtimeWebhookServer := server.NewServer(
132135
runtimeWebhookServerOpts,
136+
133137
servicelbgc.New(mgr.GetClient()),
138+
134139
calico.New(mgr.GetClient(), calicoCNIConfig),
140+
135141
httpproxy.NewVariable(),
136-
httpproxy.NewPatch(mgr.GetClient()),
142+
httpproxy.NewPatch(mgr.GetClient(), httpproxy.VariableName),
143+
137144
extraapiservercertsans.NewVariable(),
138-
extraapiservercertsans.NewPatch(),
145+
extraapiservercertsans.NewPatch(extraapiservercertsans.VariableName),
146+
147+
auditpolicy.NewPatch(),
148+
149+
clusterconfig.NewVariable(),
150+
mutation.NewMetaGeneratePatchesHandler(
151+
"clusterConfigPatch",
152+
httpproxy.NewPatch(mgr.GetClient(), clusterconfig.VariableName, httpproxy.VariableName),
153+
extraapiservercertsans.NewPatch(
154+
clusterconfig.VariableName,
155+
extraapiservercertsans.VariableName,
156+
),
157+
auditpolicy.NewPatch(),
158+
),
139159
)
140160
if err := mgr.Add(runtimeWebhookServer); err != nil {
141161
setupLog.Error(err, "unable to add runtime webhook server runnable to controller manager")
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package mutation
5+
6+
import (
7+
"context"
8+
"strings"
9+
10+
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
11+
12+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
13+
)
14+
15+
type metaGeneratePatches struct {
16+
name string
17+
wrappedHandlers []GeneratePatches
18+
}
19+
20+
func NewMetaGeneratePatchesHandler(name string, gp ...GeneratePatches) handlers.Named {
21+
return metaGeneratePatches{
22+
name: name,
23+
wrappedHandlers: gp,
24+
}
25+
}
26+
27+
func (mgp metaGeneratePatches) Name() string {
28+
return mgp.name
29+
}
30+
31+
func (mgp metaGeneratePatches) GeneratePatches(
32+
ctx context.Context,
33+
req *runtimehooksv1.GeneratePatchesRequest,
34+
resp *runtimehooksv1.GeneratePatchesResponse,
35+
) {
36+
for _, h := range mgp.wrappedHandlers {
37+
wrappedResp := &runtimehooksv1.GeneratePatchesResponse{}
38+
h.GeneratePatches(ctx, req, wrappedResp)
39+
resp.Items = append(resp.Items, wrappedResp.Items...)
40+
if wrappedResp.Message != "" {
41+
resp.Message = strings.TrimPrefix(resp.Message+"\n"+wrappedResp.Message, "\n")
42+
}
43+
resp.Status = wrappedResp.Status
44+
if resp.Status == runtimehooksv1.ResponseStatusFailure {
45+
return
46+
}
47+
}
48+
49+
if resp.Status == "" {
50+
resp.Status = runtimehooksv1.ResponseStatusSuccess
51+
}
52+
}

0 commit comments

Comments
 (0)