-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Add API server cert SANs patch #129
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
title: "API Server Certificate SANs" | ||
--- | ||
|
||
If the API server can be accessed by alternative DNS addresses then setting additional SANs on the API server | ||
certificate is necessary in order for clients to successfully validate the API server certificate. | ||
|
||
To enable the API server certificate SANs enable the `apiservercertsansvars` and `apiservercertsanspatch` external | ||
patches on `ClusterClass`. | ||
|
||
```yaml | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: ClusterClass | ||
metadata: | ||
name: <NAME> | ||
spec: | ||
patches: | ||
- name: apiserver-cert-sans | ||
external: | ||
generateExtension: "apiservercertsanspatch.<external-config-name>" | ||
discoverVariablesExtension: "apiservercertsansvars.<external-config-name>" | ||
``` | ||
|
||
On the cluster resource then specify desired certificate SANs values: | ||
|
||
```yaml | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: Cluster | ||
metadata: | ||
name: <NAME> | ||
spec: | ||
topology: | ||
variables: | ||
- name: apiServerCertSANs | ||
value: | ||
- a.b.c.example.com | ||
- d.e.f.example.com | ||
``` | ||
|
||
Applying this configuration will result in the certificate SANs being correctly set in the | ||
`KubeadmControlPlaneTemplate`. | ||
|
||
This hook is enabled by default, and can be explicitly disabled by omitting the `APIServerCertSANsVars` | ||
and `APIServerCertSANsPatch` hook from the `--runtimehooks.enabled-handlers` flag. | ||
|
||
If deploying via Helm, then this can be disabled by setting `handlers.APIServerCertSANsVars.enabled=false` and | ||
`handlers.APIServerCertSANsPatch.enabled=false`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package apiservercertsans | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/serializer" | ||
"k8s.io/apimachinery/pkg/types" | ||
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" | ||
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" | ||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
"github.com/d2iq-labs/capi-runtime-extensions/pkg/capi/clustertopology/patches" | ||
"github.com/d2iq-labs/capi-runtime-extensions/pkg/capi/clustertopology/patches/selectors" | ||
"github.com/d2iq-labs/capi-runtime-extensions/pkg/capi/clustertopology/variables" | ||
"github.com/d2iq-labs/capi-runtime-extensions/server/pkg/handlers" | ||
) | ||
|
||
const ( | ||
// HandlerNamePatch is the name of the inject handler. | ||
HandlerNamePatch = "APIServerCertSANsPatch" | ||
) | ||
|
||
type apiServerCertSANsPatchHandler struct { | ||
decoder runtime.Decoder | ||
} | ||
|
||
var ( | ||
_ handlers.NamedHandler = &apiServerCertSANsPatchHandler{} | ||
_ handlers.GeneratePatchesMutationHandler = &apiServerCertSANsPatchHandler{} | ||
) | ||
|
||
func NewPatch() *apiServerCertSANsPatchHandler { | ||
scheme := runtime.NewScheme() | ||
_ = bootstrapv1.AddToScheme(scheme) | ||
_ = controlplanev1.AddToScheme(scheme) | ||
return &apiServerCertSANsPatchHandler{ | ||
decoder: serializer.NewCodecFactory(scheme).UniversalDecoder( | ||
controlplanev1.GroupVersion, | ||
bootstrapv1.GroupVersion, | ||
), | ||
} | ||
} | ||
|
||
func (h *apiServerCertSANsPatchHandler) Name() string { | ||
return HandlerNamePatch | ||
} | ||
|
||
func (h *apiServerCertSANsPatchHandler) GeneratePatches( | ||
ctx context.Context, | ||
req *runtimehooksv1.GeneratePatchesRequest, | ||
resp *runtimehooksv1.GeneratePatchesResponse, | ||
) { | ||
topologymutation.WalkTemplates( | ||
ctx, | ||
h.decoder, | ||
req, | ||
resp, | ||
func( | ||
ctx context.Context, | ||
obj runtime.Object, | ||
vars map[string]apiextensionsv1.JSON, | ||
holderRef runtimehooksv1.HolderReference, | ||
) error { | ||
log := ctrl.LoggerFrom(ctx).WithValues( | ||
"holderRef", holderRef, | ||
) | ||
|
||
apiServerCertSANsVar, found, err := variables.Get[APIServerCertSANsVariables]( | ||
vars, | ||
VariableName, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if !found { | ||
log.Info("API server cert SANs variable not defined") | ||
return nil | ||
} | ||
|
||
return patches.Generate( | ||
obj, vars, &holderRef, selectors.ControlPlane(), log, | ||
func(obj *controlplanev1.KubeadmControlPlaneTemplate) error { | ||
log.WithValues("namespacedName", types.NamespacedName{ | ||
Name: obj.Name, | ||
Namespace: obj.Namespace, | ||
}).Info("adding API server extra cert SANs in kubeadm config spec") | ||
|
||
if obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration == nil { | ||
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration = &bootstrapv1.ClusterConfiguration{} | ||
} | ||
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.APIServer.CertSANs = apiServerCertSANsVar | ||
|
||
return nil | ||
}, | ||
) | ||
}, | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package apiservercertsans | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
. "github.com/onsi/gomega/gstruct" | ||
"gomodules.xyz/jsonpatch/v2" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" | ||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
) | ||
|
||
func TestGeneratePatches(t *testing.T) { | ||
g := NewWithT(t) | ||
h := NewPatch() | ||
req := &runtimehooksv1.GeneratePatchesRequest{} | ||
resp := &runtimehooksv1.GeneratePatchesResponse{} | ||
h.GeneratePatches(context.Background(), req, resp) | ||
g.Expect(resp.Status).To(Equal(runtimehooksv1.ResponseStatusSuccess)) | ||
g.Expect(resp.Items).To(BeEmpty()) | ||
} | ||
|
||
func TestGeneratePatches_KubeadmControlPlaneTemplate(t *testing.T) { | ||
g := NewWithT(t) | ||
h := NewPatch() | ||
req := &runtimehooksv1.GeneratePatchesRequest{ | ||
Variables: []runtimehooksv1.Variable{ | ||
newVariable( | ||
VariableName, | ||
APIServerCertSANsVariables{"a.b.c.example.com", "d.e.f.example.com"}, | ||
), | ||
}, | ||
Items: []runtimehooksv1.GeneratePatchesRequestItem{ | ||
requestItem( | ||
"1", | ||
&controlplanev1.KubeadmControlPlaneTemplate{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "KubeadmControlPlaneTemplate", | ||
APIVersion: controlplanev1.GroupVersion.String(), | ||
}, | ||
}, | ||
&runtimehooksv1.HolderReference{ | ||
Kind: "Cluster", | ||
FieldPath: "spec.controlPlaneRef", | ||
}, | ||
), | ||
}, | ||
} | ||
resp := &runtimehooksv1.GeneratePatchesResponse{} | ||
h.GeneratePatches(context.Background(), req, resp) | ||
g.Expect(resp.Status).To(Equal(runtimehooksv1.ResponseStatusSuccess)) | ||
g.Expect(resp.Items).To(ContainElement(MatchFields(IgnoreExtras, Fields{ | ||
"UID": Equal(types.UID("1")), | ||
"PatchType": Equal(runtimehooksv1.JSONPatchType), | ||
"Patch": WithTransform( | ||
func(data []byte) ([]jsonpatch.Operation, error) { | ||
operations := []jsonpatch.Operation{} | ||
if err := json.Unmarshal(data, &operations); err != nil { | ||
return nil, err | ||
} | ||
return operations, nil | ||
}, | ||
ConsistOf(MatchAllFields(Fields{ | ||
"Operation": Equal("add"), | ||
"Path": Equal("/spec/template/spec/kubeadmConfigSpec/clusterConfiguration"), | ||
"Value": HaveKeyWithValue( | ||
"apiServer", | ||
HaveKeyWithValue( | ||
"certSANs", | ||
[]interface{}{"a.b.c.example.com", "d.e.f.example.com"}, | ||
), | ||
jimmidyson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
})), | ||
), | ||
}))) | ||
} | ||
|
||
func toJSON(v any) []byte { | ||
data, err := json.Marshal(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
compacted := &bytes.Buffer{} | ||
if err := json.Compact(compacted, data); err != nil { | ||
panic(err) | ||
} | ||
return compacted.Bytes() | ||
} | ||
|
||
// requestItem returns a GeneratePatchesRequestItem with the given uid, variables and object. | ||
func requestItem( | ||
uid string, | ||
object any, | ||
holderRef *runtimehooksv1.HolderReference, | ||
) runtimehooksv1.GeneratePatchesRequestItem { | ||
return runtimehooksv1.GeneratePatchesRequestItem{ | ||
UID: types.UID(uid), | ||
Object: runtime.RawExtension{ | ||
Raw: toJSON(object), | ||
}, | ||
HolderReference: *holderRef, | ||
} | ||
} | ||
|
||
// newVariable returns a runtimehooksv1.Variable with the passed name and value. | ||
func newVariable(name string, value any) runtimehooksv1.Variable { | ||
return runtimehooksv1.Variable{ | ||
Name: name, | ||
Value: apiextensionsv1.JSON{Raw: toJSON(value)}, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.