-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add httpproxy external patch #115
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 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5bb4f6c
feat: add httpproxy external patch
mhrabovcin 355046a
feat: add unique name multiplexing
mhrabovcin e2827e9
fix: go mod tidy
mhrabovcin 4463506
feat: http-proxy docs
mhrabovcin 48301ea
fix: linter issues
mhrabovcin 76f7a8e
chore: address PR feedback
mhrabovcin 577364f
fix: linter
mhrabovcin 6cfc10d
fix: update docs
mhrabovcin 06894ce
fix: linter
mhrabovcin 5b749f5
chore: add http proxy handler logging
mhrabovcin 8bc0277
fix: improve docs
mhrabovcin 05e6646
chore: improve code readability
mhrabovcin d1a11a8
chore: improve logging context
mhrabovcin 518b58a
fix: rename handler names
mhrabovcin 48c3ca1
fix: helm generated docs
mhrabovcin 0639776
fix: http proxy patch handler name
mhrabovcin fc8b789
chore: update kind to latest version
mhrabovcin 0881a6b
fix: proxy patch handler name
mhrabovcin 152357b
Merge remote-tracking branch 'origin/main' into mh/ep-http-proxy
jimmidyson 7f26649
fixup! refactor: Satisfy linter
jimmidyson 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: "HTTP proxy for CAPI components" | ||
--- | ||
|
||
In some network environments it is necessary to use HTTP proxy to successfuly execute HTTP requests. | ||
To configure Kubernetes components (`containerd`, `kubelet`) to use HTTP proxy use the `http-proxy` | ||
external patch that will generate appropriate configuration for control plane and worker nodes. | ||
|
||
To enable the http proxy enable the `http-proxy` external patch on `ClusterClass`. | ||
|
||
```yaml | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: ClusterClass | ||
metadata: | ||
name: <NAME> | ||
spec: | ||
patches: | ||
- name: http-proxy | ||
external: | ||
generateExtension: "http-proxy-patch.<external-config-name>" | ||
discoverVariablesExtension: "http-proxy-vars.<external-config-name>" | ||
``` | ||
|
||
On the cluster resource then specify desired HTTP proxy values: | ||
|
||
```yaml | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: Cluster | ||
metadata: | ||
name: <NAME> | ||
spec: | ||
topology: | ||
variables: | ||
name: proxy | ||
values: | ||
http: http://example.com | ||
https: http://example.com | ||
no: | ||
- http://no-proxy-1.example.com | ||
- http://no-proxy-2.example.com | ||
``` | ||
|
||
Applying this configuration will result in new bootstrap files on the `KubeadmControlPlaneTemplate` | ||
and `KubeadmConfigTemplate`. |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,150 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package httpproxyconfig | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/serializer" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
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" | ||
|
||
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers" | ||
) | ||
|
||
const ( | ||
// VariableName is http proxy external patch variable name. | ||
VariableName = "proxy" | ||
) | ||
|
||
type httpProxyConfigHandler struct { | ||
decoder runtime.Decoder | ||
generator *systemdConfigGenerator | ||
} | ||
|
||
var ( | ||
_ handlers.NamedHandler = &httpProxyConfigHandler{} | ||
_ handlers.DiscoverVariablesMutationHandler = &httpProxyConfigHandler{} | ||
_ handlers.GeneratePatchesMutationHandler = &httpProxyConfigHandler{} | ||
) | ||
|
||
func New() *httpProxyConfigHandler { | ||
scheme := runtime.NewScheme() | ||
_ = bootstrapv1.AddToScheme(scheme) | ||
_ = controlplanev1.AddToScheme(scheme) | ||
return &httpProxyConfigHandler{ | ||
decoder: serializer.NewCodecFactory(scheme).UniversalDecoder( | ||
controlplanev1.GroupVersion, | ||
bootstrapv1.GroupVersion, | ||
), | ||
generator: &systemdConfigGenerator{ | ||
template: templates.Lookup("systemd.conf.tmpl"), | ||
}, | ||
} | ||
} | ||
|
||
func (h *httpProxyConfigHandler) Name() string { | ||
return "http-proxy" | ||
} | ||
|
||
func (h *httpProxyConfigHandler) GeneratePatches( | ||
ctx context.Context, | ||
req *runtimehooksv1.GeneratePatchesRequest, | ||
resp *runtimehooksv1.GeneratePatchesResponse, | ||
) { | ||
topologymutation.WalkTemplates( | ||
ctx, | ||
h.decoder, | ||
req, | ||
resp, | ||
func(ctx context.Context, obj runtime.Object, variables map[string]apiextensionsv1.JSON, holderRef runtimehooksv1.HolderReference) error { | ||
httpProxyVariable, found, err := GetVariable[HTTPProxyVariables]( | ||
variables, | ||
VariableName, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if !found { | ||
return fmt.Errorf("missing variable %q value", VariableName) | ||
} | ||
|
||
controlPlaneSelector := clusterv1.PatchSelector{ | ||
APIVersion: controlplanev1.GroupVersion.String(), | ||
Kind: "KubeadmControlPlaneTemplate", | ||
MatchResources: clusterv1.PatchSelectorMatch{ | ||
ControlPlane: true, | ||
}, | ||
} | ||
if err := generatePatch(obj, variables, holderRef, controlPlaneSelector, func(obj *controlplanev1.KubeadmControlPlaneTemplate) error { | ||
var err error | ||
obj.Spec.Template.Spec.KubeadmConfigSpec.Files, err = h.generator.AddSystemdFiles( | ||
httpProxyVariable, obj.Spec.Template.Spec.KubeadmConfigSpec.Files) | ||
return err | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
defaultWorkerSelector := clusterv1.PatchSelector{ | ||
APIVersion: bootstrapv1.GroupVersion.String(), | ||
Kind: "KubeadmConfigTemplate", | ||
MatchResources: clusterv1.PatchSelectorMatch{ | ||
MachineDeploymentClass: &clusterv1.PatchSelectorMatchMachineDeploymentClass{ | ||
Names: []string{ | ||
"default-worker", | ||
}, | ||
}, | ||
}, | ||
} | ||
if err := generatePatch(obj, variables, holderRef, defaultWorkerSelector, func(obj *bootstrapv1.KubeadmConfigTemplate) error { | ||
var err error | ||
obj.Spec.Template.Spec.Files, err = h.generator.AddSystemdFiles(httpProxyVariable, obj.Spec.Template.Spec.Files) | ||
return err | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
) | ||
} | ||
|
||
func (h *httpProxyConfigHandler) DiscoverVariables( | ||
ctx context.Context, | ||
_ *runtimehooksv1.DiscoverVariablesRequest, | ||
resp *runtimehooksv1.DiscoverVariablesResponse, | ||
) { | ||
variable := HTTPProxyVariables{} | ||
resp.Variables = append(resp.Variables, clusterv1.ClusterClassVariable{ | ||
Name: VariableName, | ||
Required: false, | ||
Schema: variable.VariableSchema(), | ||
}) | ||
resp.SetStatus(runtimehooksv1.ResponseStatusSuccess) | ||
} | ||
|
||
func generatePatch[T runtime.Object]( | ||
obj runtime.Object, | ||
variables map[string]apiextensionsv1.JSON, | ||
holderRef runtimehooksv1.HolderReference, | ||
patchSelector clusterv1.PatchSelector, | ||
mutFn func(T) error, | ||
) error { | ||
typed, ok := obj.(T) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
if !matchSelector(patchSelector, obj, holderRef, variables) { | ||
return nil | ||
} | ||
|
||
return mutFn(typed) | ||
} |
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.