Skip to content

Commit 55f14b7

Browse files
authored
Merge pull request #593 from pjbgf/new-kube-flag
Add kubeconfig flags
2 parents 412877c + d29032e commit 55f14b7

9 files changed

Lines changed: 46 additions & 22 deletions

File tree

.github/workflows/e2e.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ jobs:
5252
uses: fluxcd/pkg/actions/kubectl@main
5353
with:
5454
version: 1.21.2
55-
- name: Setup SOPS
56-
uses: fluxcd/pkg/actions/sops@main
5755
- name: Enable integration tests
5856
# Only run integration tests for main branch
5957
if: github.ref == 'refs/heads/main'

DEVELOPMENT.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ In addition to the above, the following dependencies are also used by some of th
1818
- `controller-gen` (v0.7.0)
1919
- `gen-crd-api-reference-docs` (v0.3.0)
2020
- `setup-envtest` (latest)
21+
- `sops` (v3.7.2)
2122

2223
If any of the above dependencies are not present on your system, the first invocation of a `make` target that requires them will install them.
2324

Makefile

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ IMG ?= fluxcd/kustomize-controller:latest
44
CRD_OPTIONS ?= crd:crdVersions=v1
55
SOURCE_VER ?= v0.22.3
66

7-
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
7+
# Use the same version of SOPS already referenced on go.mod
8+
SOPS_VER := $(shell go list -m all | grep go.mozilla.org/sops | awk '{print $$2}')
9+
10+
# Repository root based on Git metadata
11+
REPOSITORY_ROOT := $(shell git rev-parse --show-toplevel)
12+
BUILD_DIR := $(REPOSITORY_ROOT)/build
13+
14+
# If gobin not set, create one on ./build and add to path.
815
ifeq (,$(shell go env GOBIN))
9-
GOBIN=$(shell go env GOPATH)/bin
16+
GOBIN=$(BUILD_DIR)/gobin
1017
else
1118
GOBIN=$(shell go env GOBIN)
1219
endif
20+
export PATH:=$(GOBIN):${PATH}
1321

1422
# Allows for defining additional Go test args, e.g. '-tags integration'.
1523
GO_TEST_ARGS ?=
@@ -25,20 +33,24 @@ ENVTEST_ARCH ?= amd64
2533
all: manager
2634

2735
# Download the envtest binaries to testbin
28-
ENVTEST_ASSETS_DIR=$(shell pwd)/build/testbin
36+
ENVTEST_ASSETS_DIR=$(BUILD_DIR)/testbin
2937
ENVTEST_KUBERNETES_VERSION?=latest
3038
install-envtest: setup-envtest
3139
mkdir -p ${ENVTEST_ASSETS_DIR}
3240
$(ENVTEST) use $(ENVTEST_KUBERNETES_VERSION) --arch=$(ENVTEST_ARCH) --bin-dir=$(ENVTEST_ASSETS_DIR)
3341

42+
SOPS = $(GOBIN)/sops
43+
$(SOPS): ## Download latest sops binary if none is found.
44+
$(call go-install-tool,$(SOPS),go.mozilla.org/sops/v3/cmd/sops@$(SOPS_VER))
45+
3446
# Run controller tests
3547
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
36-
test: tidy generate fmt vet manifests api-docs download-crd-deps install-envtest
48+
test: tidy generate fmt vet manifests api-docs download-crd-deps install-envtest $(SOPS)
3749
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) go test ./... $(GO_TEST_ARGS) -v -coverprofile cover.out
3850

3951
# Build manager binary
4052
manager: generate fmt vet
41-
go build -o bin/manager main.go
53+
go build -o $(BUILD_DIR)/bin/manager main.go
4254

4355
# Run against the configured Kubernetes cluster in ~/.kube/config
4456
run: generate fmt vet manifests
@@ -120,18 +132,18 @@ docker-deploy:
120132
kubectl -n flux-system set image deployment/kustomize-controller manager=${IMG}
121133

122134
# Find or download controller-gen
123-
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
135+
CONTROLLER_GEN = $(GOBIN)/controller-gen
124136
.PHONY: controller-gen
125137
controller-gen: ## Download controller-gen locally if necessary.
126138
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0)
127139

128140
# Find or download gen-crd-api-reference-docs
129-
GEN_CRD_API_REFERENCE_DOCS = $(shell pwd)/bin/gen-crd-api-reference-docs
141+
GEN_CRD_API_REFERENCE_DOCS = $(GOBIN)/gen-crd-api-reference-docs
130142
.PHONY: gen-crd-api-reference-docs
131143
gen-crd-api-reference-docs:
132144
$(call go-install-tool,$(GEN_CRD_API_REFERENCE_DOCS),github.com/ahmetb/gen-crd-api-reference-docs@v0.3.0)
133145

134-
ENVTEST = $(shell pwd)/bin/setup-envtest
146+
ENVTEST = $(GOBIN)/setup-envtest
135147
.PHONY: envtest
136148
setup-envtest: ## Download envtest-setup locally if necessary.
137149
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
@@ -145,26 +157,26 @@ TMP_DIR=$$(mktemp -d) ;\
145157
cd $$TMP_DIR ;\
146158
go mod init tmp ;\
147159
echo "Downloading $(2)" ;\
148-
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
160+
GOBIN=$(GOBIN) go install $(2) ;\
149161
rm -rf $$TMP_DIR ;\
150162
}
151163
endef
152164

153165
# Build fuzzers
154166
fuzz-build:
155-
rm -rf $(shell pwd)/build/fuzz/
156-
mkdir -p $(shell pwd)/build/fuzz/out/
167+
rm -rf $(BUILD_DIR)/fuzz/
168+
mkdir -p $(BUILD_DIR)/fuzz/out/
157169

158170
docker build . --tag local-fuzzing:latest -f tests/fuzz/Dockerfile.builder
159171
docker run --rm \
160172
-e FUZZING_LANGUAGE=go -e SANITIZER=address \
161173
-e CIFUZZ_DEBUG='True' -e OSS_FUZZ_PROJECT_NAME=fluxcd \
162-
-v "$(shell pwd)/build/fuzz/out":/out \
174+
-v "$(BUILD_DIR)/fuzz/out":/out \
163175
local-fuzzing:latest
164176

165177
fuzz-smoketest: fuzz-build
166178
docker run --rm \
167-
-v "$(shell pwd)/build/fuzz/out":/out \
179+
-v "$(BUILD_DIR)/fuzz/out":/out \
168180
-v "$(shell pwd)/tests/fuzz/oss_fuzz_run.sh":/runner.sh \
169181
local-fuzzing:latest \
170182
bash -c "/runner.sh"

controllers/kustomization_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import (
5757
apiacl "github.com/fluxcd/pkg/apis/acl"
5858
"github.com/fluxcd/pkg/apis/meta"
5959
"github.com/fluxcd/pkg/runtime/acl"
60+
runtimeClient "github.com/fluxcd/pkg/runtime/client"
6061
"github.com/fluxcd/pkg/runtime/events"
6162
"github.com/fluxcd/pkg/runtime/metrics"
6263
"github.com/fluxcd/pkg/runtime/predicates"
@@ -88,6 +89,7 @@ type KustomizationReconciler struct {
8889
statusManager string
8990
NoCrossNamespaceRefs bool
9091
DefaultServiceAccount string
92+
KubeConfigOpts runtimeClient.KubeConfigOptions
9193
}
9294

9395
// KustomizationReconcilerOptions contains options for the KustomizationReconciler.
@@ -343,7 +345,7 @@ func (r *KustomizationReconciler) reconcile(
343345
}
344346

345347
// setup the Kubernetes client for impersonation
346-
impersonation := NewKustomizeImpersonation(kustomization, r.Client, r.StatusPoller, r.DefaultServiceAccount)
348+
impersonation := NewKustomizeImpersonation(kustomization, r.Client, r.StatusPoller, r.DefaultServiceAccount, r.KubeConfigOpts)
347349
kubeClient, statusPoller, err := impersonation.GetClient(ctx)
348350
if err != nil {
349351
return kustomizev1.KustomizationNotReady(
@@ -926,7 +928,7 @@ func (r *KustomizationReconciler) finalize(ctx context.Context, kustomization ku
926928
kustomization.Status.Inventory.Entries != nil {
927929
objects, _ := ListObjectsInInventory(kustomization.Status.Inventory)
928930

929-
impersonation := NewKustomizeImpersonation(kustomization, r.Client, r.StatusPoller, r.DefaultServiceAccount)
931+
impersonation := NewKustomizeImpersonation(kustomization, r.Client, r.StatusPoller, r.DefaultServiceAccount, r.KubeConfigOpts)
930932
if impersonation.CanFinalize(ctx) {
931933
kubeClient, _, err := impersonation.GetClient(ctx)
932934
if err != nil {

controllers/kustomization_impersonation.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
"sigs.k8s.io/controller-runtime/pkg/client/config"
3232

3333
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
34+
35+
runtimeClient "github.com/fluxcd/pkg/runtime/client"
3436
)
3537

3638
// KustomizeImpersonation holds the state for impersonating a service account.
@@ -39,19 +41,22 @@ type KustomizeImpersonation struct {
3941
kustomization kustomizev1.Kustomization
4042
statusPoller *polling.StatusPoller
4143
defaultServiceAccount string
44+
kubeConfigOpts runtimeClient.KubeConfigOptions
4245
}
4346

4447
// NewKustomizeImpersonation creates a new KustomizeImpersonation.
4548
func NewKustomizeImpersonation(
4649
kustomization kustomizev1.Kustomization,
4750
kubeClient client.Client,
4851
statusPoller *polling.StatusPoller,
49-
defaultServiceAccount string) *KustomizeImpersonation {
52+
defaultServiceAccount string,
53+
kubeConfigOpts runtimeClient.KubeConfigOptions) *KustomizeImpersonation {
5054
return &KustomizeImpersonation{
5155
defaultServiceAccount: defaultServiceAccount,
5256
kustomization: kustomization,
5357
statusPoller: statusPoller,
5458
Client: kubeClient,
59+
kubeConfigOpts: kubeConfigOpts,
5560
}
5661
}
5762

@@ -141,6 +146,8 @@ func (ki *KustomizeImpersonation) clientForKubeConfig(ctx context.Context) (clie
141146
if err != nil {
142147
return nil, nil, err
143148
}
149+
150+
restConfig = runtimeClient.KubeConfig(restConfig, ki.kubeConfigOpts)
144151
ki.setImpersonationConfig(restConfig)
145152

146153
restMapper, err := apiutil.NewDynamicRESTMapper(restConfig)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/fluxcd/pkg/apis/acl v0.0.3
1818
github.com/fluxcd/pkg/apis/kustomize v0.3.2
1919
github.com/fluxcd/pkg/apis/meta v0.12.1
20-
github.com/fluxcd/pkg/runtime v0.13.2
20+
github.com/fluxcd/pkg/runtime v0.13.3
2121
github.com/fluxcd/pkg/ssa v0.15.1
2222
github.com/fluxcd/pkg/testserver v0.2.0
2323
github.com/fluxcd/pkg/untar v0.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ github.com/fluxcd/pkg/apis/kustomize v0.3.2 h1:ULoAwOOekHf5cy6mYIwL+K6v8/cfcNVVb
278278
github.com/fluxcd/pkg/apis/kustomize v0.3.2/go.mod h1:p8iAH5TeqMBnnxkkpCNNDvWYfKlNRx89a6WKOo+hJHA=
279279
github.com/fluxcd/pkg/apis/meta v0.12.1 h1:m5PfKAqbqWBvGp9+JRj1sv+xNkGsHwUVf+3rJ8wm6SE=
280280
github.com/fluxcd/pkg/apis/meta v0.12.1/go.mod h1:f8YVt70/KAhqzZ7xxhjvqyzKubOYx2pAbakb/FfCEg8=
281-
github.com/fluxcd/pkg/runtime v0.13.2 h1:6jkQQUbp17WxHsbozlJFCvHmOS4JIB+yB20CdCd8duE=
282-
github.com/fluxcd/pkg/runtime v0.13.2/go.mod h1:dzWNKqFzFXeittbpFcJzR3cdC9CWlbzw+pNOgaVvF/0=
281+
github.com/fluxcd/pkg/runtime v0.13.3 h1:k0Xun+RoEC/F6iuAPTA6rQb+I4B4oecBx6pOcodX11A=
282+
github.com/fluxcd/pkg/runtime v0.13.3/go.mod h1:dzWNKqFzFXeittbpFcJzR3cdC9CWlbzw+pNOgaVvF/0=
283283
github.com/fluxcd/pkg/ssa v0.15.1 h1:HXAT+K6c9Yy8Evxdyk3DU0KTk3yZ+fwgTEEzU1W/1V8=
284284
github.com/fluxcd/pkg/ssa v0.15.1/go.mod h1:OSXVu/uKPbhzBRljA359+WYxbXtMUNbkADlrS3Rm+gE=
285285
github.com/fluxcd/pkg/testserver v0.2.0 h1:Mj0TapmKaywI6Fi5wvt1LAZpakUHmtzWQpJNKQ0Krt4=

internal/sops/azkv/keysource_integration_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// +tag integration
1+
//go:build integration
2+
// +build integration
23

34
/*
45
Copyright 2022 The Flux authors

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func main() {
6868
concurrent int
6969
requeueDependency time.Duration
7070
clientOptions client.Options
71+
kubeConfigOpts client.KubeConfigOptions
7172
logOptions logger.Options
7273
leaderElectionOptions leaderelection.Options
7374
aclOptions acl.Options
@@ -89,6 +90,7 @@ func main() {
8990
logOptions.BindFlags(flag.CommandLine)
9091
leaderElectionOptions.BindFlags(flag.CommandLine)
9192
aclOptions.BindFlags(flag.CommandLine)
93+
kubeConfigOpts.BindFlags(flag.CommandLine)
9294
flag.Parse()
9395

9496
ctrl.SetLogger(logger.NewLogger(logOptions))
@@ -139,6 +141,7 @@ func main() {
139141
MetricsRecorder: metricsRecorder,
140142
StatusPoller: polling.NewStatusPoller(mgr.GetClient(), mgr.GetRESTMapper(), polling.Options{}),
141143
NoCrossNamespaceRefs: aclOptions.NoCrossNamespaceRefs,
144+
KubeConfigOpts: kubeConfigOpts,
142145
}).SetupWithManager(mgr, controllers.KustomizationReconcilerOptions{
143146
MaxConcurrentReconciles: concurrent,
144147
DependencyRequeueInterval: requeueDependency,

0 commit comments

Comments
 (0)