Skip to content

Commit 714f8af

Browse files
authored
Merge pull request #26 from sp-yduck/secretref
Secretref
2 parents 8ca0f58 + 8d20ae8 commit 714f8af

19 files changed

+239
-261
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ Dockerfile.cross
2525
*.swo
2626
*~
2727

28-
2928
*_test.go
3029

31-
#
32-
config/samples/secret.yaml
30+
# release
31+
out

Makefile

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14+
GOARCH := $(shell go env GOARCH)
15+
GOOS := $(shell go env GOOS)
16+
1417
# Setting SHELL to bash allows bash commands to be executed by recipes.
1518
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
1619
SHELL = /usr/bin/env bash -o pipefail
@@ -58,6 +61,16 @@ vet: ## Run go vet against code.
5861
test: manifests generate fmt vet envtest ## Run tests.
5962
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
6063

64+
CLUSTER_NAME := cappx-test
65+
66+
.PHONY: create-workload-cluster
67+
create-workload-cluster: $(KUSTOMIZE) $(ENVSUBST) $(KUBECTL)
68+
export CLUSTER_NAME=$(CLUSTER_NAME) && $(KUSTOMIZE) build templates | $(ENVSUBST) | $(KUBECTL) apply -f -
69+
70+
.PHONY: delete-workload-cluster
71+
delete-workload-cluster: $(KUBECTL)
72+
$(KUBECTL) delete cluster $(CLUSTER_NAME)
73+
6174
##@ Build
6275

6376
.PHONY: build
@@ -72,7 +85,7 @@ run: manifests generate fmt vet ## Run a controller from your host.
7285
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
7386
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
7487
.PHONY: docker-build
75-
docker-build: test ## Build docker image with the manager.
88+
docker-build: # test ## Build docker image with the manager.
7689
docker build -t ${IMG} .
7790

7891
.PHONY: docker-push
@@ -119,6 +132,30 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
119132
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
120133
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
121134

135+
##@ Release
136+
137+
## Location to output for release
138+
RELEASE_DIR := out
139+
$(RELEASE_DIR):
140+
mkdir -p $(RELEASE_DIR)
141+
142+
# RELEASE_TAG := $(shell git describe --abbrev=0 2>/dev/null)
143+
144+
# .PHONY: release
145+
146+
.PHONY: release-manifests
147+
release-manifests: $(KUSTOMIZE) $(RELEASE_DIR) ## Builds the manifests to publish with a release
148+
$(KUSTOMIZE) build config/default > $(RELEASE_DIR)/infrastructure-components.yaml
149+
150+
.PHONY: release-metadata
151+
release-metadata: $(RELEASE_DIR)
152+
cp metadata.yaml $(RELEASE_DIR)/metadata.yaml
153+
154+
.PHONY: release-templates
155+
release-templates: $(RELEASE_DIR)
156+
cp templates/cluster-template* $(RELEASE_DIR)/
157+
158+
122159
##@ Build Dependencies
123160

124161
## Location to install dependencies to
@@ -130,10 +167,14 @@ $(LOCALBIN):
130167
KUSTOMIZE ?= $(LOCALBIN)/kustomize
131168
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
132169
ENVTEST ?= $(LOCALBIN)/setup-envtest
170+
ENVSUBST ?= $(LOCALBIN)/envsubst
171+
KUBECTL ?= $(LOCALBIN)/kubectl
133172

134173
## Tool Versions
135174
KUSTOMIZE_VERSION ?= v5.0.0
136175
CONTROLLER_TOOLS_VERSION ?= v0.11.3
176+
ENVSUBST_VER ?= v1.4.2
177+
KUBECTL_VER := v1.25.10
137178

138179
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
139180
.PHONY: kustomize
@@ -155,3 +196,14 @@ $(CONTROLLER_GEN): $(LOCALBIN)
155196
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
156197
$(ENVTEST): $(LOCALBIN)
157198
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
199+
200+
.PHONY: envsubst
201+
envsubst: $(ENVSUBST)
202+
$(ENVSUBST): $(LOCALBIN)
203+
GOBIN=$(LOCALBIN) go install github.com/a8m/envsubst/cmd/envsubst@$(ENVSUBST_VER)
204+
205+
.PHONY: kubectl
206+
kubectl: $(KUBECTL)
207+
$(KUBECTL): $(LOCALBIN)
208+
curl --retry 3 -fsL https://dl.k8s.io/release/$(KUBECTL_VER)/bin/$(GOOS)/$(GOARCH)/kubectl -o $(LOCALBIN)/kubectl
209+
chmod +x $(KUBECTL)

README.md

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,36 @@ make deploy
2525

2626
2. Create your first workload cluster
2727
```sh
28-
# cluster & infra cluster
29-
kubectl apply -f config/samples/cluster.yaml
30-
kubectl apply -f config/samples/infrastructure_v1beta1_proxmoxcluster.yaml
28+
# export env variables
29+
export CONTROLPLANE_HOST=X.X.X.X
30+
export PROXMOX_URL=X.X.X.X:8006
31+
export GATEWAY_ADDRESS=X.X.X.X
32+
export NAMESERVER_ADDRESS=X.X.X.X
33+
export PROXMOX_PASSWORD_BASE64=$(echo -n <password> | base64)
34+
export PROXMOX_USER_BASE64=$(echo -n <user@pam> | base64)
35+
export NODE_URL_BASE64=$(echo -n <node.ssh.url:22> | base64)
36+
export NODE_USER_BASE64=$(echo -n <node-ssh-user> | base64)
37+
export NODE_PASSWORD_BASE64=$(echo -n <node-ssh-password> | base64)
38+
39+
make create-workload-cluster
40+
```
3141

32-
# controlplane
33-
kubectl apply -f config/samples/controlplane.yaml
42+
3. Access your first workload cluster !!
3443

35-
# machine & bootstrap & infra machine
36-
kubectl apply -f config/samples/machine.yaml
37-
kubectl apply -f config/samples/bootstrap.yaml
38-
kubectl apply -f config/samples/infrastructure_v1beta1_proxmoxcluster.yaml
44+
Usually it takes 2~5 mins to complete bootstrap the nodes.
45+
```sh
46+
# get workload cluster's kubeconfig
47+
clusterctl get kubeconfig cappx-test > kubeconfig.yaml
3948

40-
# proxmox configs
41-
kubetl apply -f <your-proxmox-config-secret>.yaml
49+
# get node command for workload cluster
50+
kubectl --kubeconfig=kubeconfig.yaml get node
4251
```
4352

44-
You need to provide your proxmox information through secret.
45-
```yaml
46-
# <your-proxmox-config-secret>.yaml
47-
apiVersion: v1
48-
data:
49-
# for proxmox API
50-
PROXMOX_PASSWORD: "<base 64>"
51-
PROXMOX_USER: "<base 64>"
52-
# for ssh into the node to bootstrapping VMs
53-
## * current CAPP is compatible with only single node proxmox cluster
54-
NODE_URL: "<base 64>"
55-
NODE_USER: "<base 64>"
56-
NODE_PASSWORD: "<base 64>"
57-
kind: Secret
58-
metadata:
59-
name: proxmoxcluster-sample
60-
type: Opaque
53+
4. Tear down your workload cluster
54+
```sh
55+
make delete-workload-cluster
6156
```
6257

63-
3. Check your Cluster & Machines !!
64-
65-
Once CAPP reconciled your `ProxmoxCluster`/`ProxmoxMachine`, you can see `READY=true` for `ProxmoxCluster` and `STATUS=running` for `ProxmoxMachine`.
66-
67-
![kubectl-get-proxmox-cluster](./logos/k-get-proxmoxcluster.PNG)
68-
69-
![kubectl-get-proxmox-machine](./logos/k-get-proxmoxmachine.PNG)
70-
7158
## Fetures
7259

7360
- No need to prepare vm templates. You can specify any vm image in `ProxmoxMachine.Spec.Image`.

api/v1beta1/type.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@ type ServerRef struct {
1717

1818
// to do : client options like insecure tls verify
1919

20-
// CredentialsRef is a reference for secret which contains proxmox login secrets
20+
// SecretRef is a reference for secret which contains proxmox login secrets
2121
// and ssh configs for proxmox nodes
22-
CredentialsRef *ObjectReference `json:"credentialsRef"`
22+
SecretRef *ObjectReference `json:"secretRef"`
2323
}
2424

2525
// NodeRef
2626
type NodeRef struct {
27-
Name string `json:"name"`
28-
CredentialsRef *ObjectReference `json:"credentialsRef"`
27+
Name string `json:"name"`
28+
SecretRef *ObjectReference `json:"secretRef"`
2929
}
3030

3131
// ObjectReference is a reference to another Kubernetes object instance.
3232
type ObjectReference struct {
3333
// Namespace of the referent.
3434
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
35-
// +kubebuilder:validation:Required
36-
Namespace string `json:"namespace"`
35+
Namespace string `json:"namespace,omitempty"`
3736
// Name of the referent.
3837
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
3938
// +kubebuilder:validation:Required

api/v1beta1/zz_generated.deepcopy.go

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

cloud/scope/clients.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ type ProxmoxServices struct {
3333
}
3434

3535
func newComputeService(ctx context.Context, serverRef infrav1.ServerRef, crClient client.Client) (*service.Service, error) {
36-
credentialsRef := serverRef.CredentialsRef
37-
if credentialsRef == nil {
38-
return nil, errors.New("failed to get proxmox client form nil credentialsRef")
36+
secretRef := serverRef.SecretRef
37+
if secretRef == nil {
38+
return nil, errors.New("failed to get proxmox client form nil secretRef")
3939
}
4040

4141
var secret corev1.Secret
42-
key := client.ObjectKey{Namespace: credentialsRef.Namespace, Name: credentialsRef.Name}
42+
key := client.ObjectKey{Namespace: secretRef.Namespace, Name: secretRef.Name}
4343
if err := crClient.Get(ctx, key, &secret); err != nil {
4444
return nil, err
4545
}
@@ -56,13 +56,13 @@ func newComputeService(ctx context.Context, serverRef infrav1.ServerRef, crClien
5656
return service.NewServiceWithLogin(serverRef.Endpoint, string(proxmoxUser), string(proxmoxPassword))
5757
}
5858

59-
func newRemoteClient(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client) (*SSHClient, error) {
60-
if credentialsRef == nil {
61-
return nil, errors.New("failed to get proxmox client form nil credentialsRef")
59+
func newRemoteClient(ctx context.Context, secretRef *infrav1.ObjectReference, crClient client.Client) (*SSHClient, error) {
60+
if secretRef == nil {
61+
return nil, errors.New("failed to get proxmox client form nil secretRef")
6262
}
6363

6464
var secret corev1.Secret
65-
key := client.ObjectKey{Namespace: credentialsRef.Namespace, Name: credentialsRef.Name}
65+
key := client.ObjectKey{Namespace: secretRef.Namespace, Name: secretRef.Name}
6666
if err := crClient.Get(ctx, key, &secret); err != nil {
6767
return nil, err
6868
}

cloud/scope/cluster.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func NewClusterScope(ctx context.Context, params ClusterScopeParams) (*ClusterSc
4242
if params.ProxmoxCluster == nil {
4343
return nil, errors.New("failed to generate new scope from nil ProxmoxCluster")
4444
}
45+
populateNamespace(params.ProxmoxCluster)
4546

4647
if params.ProxmoxServices.Compute == nil {
4748
computeSvc, err := newComputeService(ctx, params.ProxmoxCluster.Spec.ServerRef, params.Client)
@@ -53,7 +54,7 @@ func NewClusterScope(ctx context.Context, params ClusterScopeParams) (*ClusterSc
5354

5455
if params.ProxmoxServices.Remote == nil {
5556
// current CAPP is compatible with only single node proxmox cluster
56-
remote, err := newRemoteClient(ctx, params.ProxmoxCluster.Spec.NodeRefs[0].CredentialsRef, params.Client)
57+
remote, err := newRemoteClient(ctx, params.ProxmoxCluster.Spec.NodeRefs[0].SecretRef, params.Client)
5758
if err != nil {
5859
return nil, errors.Errorf("failed to create remote client: %v", err)
5960
}
@@ -74,6 +75,17 @@ func NewClusterScope(ctx context.Context, params ClusterScopeParams) (*ClusterSc
7475
}, err
7576
}
7677

78+
func populateNamespace(proxmoxCluster *infrav1.ProxmoxCluster) {
79+
if proxmoxCluster.Spec.ServerRef.SecretRef.Namespace == "" {
80+
proxmoxCluster.Spec.ServerRef.SecretRef.Namespace = proxmoxCluster.Namespace
81+
}
82+
for i, nodeRef := range proxmoxCluster.Spec.NodeRefs {
83+
if nodeRef.SecretRef.Namespace == "" {
84+
proxmoxCluster.Spec.NodeRefs[i].SecretRef.Namespace = proxmoxCluster.Namespace
85+
}
86+
}
87+
}
88+
7789
type ClusterScope struct {
7890
ProxmoxServices
7991
client client.Client

cloud/services/compute/instance/cloudinit.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ func (s *Service) reconcileCloudInitUser(bootstrap string) error {
5959
return err
6060
}
6161
base := baseUserData(vmName)
62-
additional, err := cloudinit.MergeUsers(*config, base)
63-
if err != nil {
64-
return err
62+
if config != nil {
63+
base, err = cloudinit.MergeUsers(*config, *base)
64+
if err != nil {
65+
return err
66+
}
6567
}
66-
cloudConfig, err := cloudinit.MergeUsers(*additional, *bootstrapConfig)
68+
cloudConfig, err := cloudinit.MergeUsers(*base, *bootstrapConfig)
6769
if err != nil {
6870
return err
6971
}
@@ -101,8 +103,8 @@ func ApplyCICustom(vmid int, vmName, storageName, ciType string, ssh scope.SSHCl
101103
}
102104

103105
// to do : remove these cloud-config
104-
func baseUserData(vmName string) infrav1.User {
105-
return infrav1.User{
106+
func baseUserData(vmName string) *infrav1.User {
107+
return &infrav1.User{
106108
GrowPart: infrav1.GrowPart{Mode: "auto", Devices: []string{"/"}, IgnoreGrowrootDisabled: false},
107109
HostName: vmName,
108110
ManageEtcHosts: true,

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxclusters.yaml

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ spec:
7272
items:
7373
description: NodeRef
7474
properties:
75-
credentialsRef:
75+
name:
76+
type: string
77+
secretRef:
7678
description: ObjectReference is a reference to another Kubernetes
7779
object instance.
7880
properties:
@@ -84,20 +86,21 @@ spec:
8486
type: string
8587
required:
8688
- name
87-
- namespace
8889
type: object
89-
name:
90-
type: string
9190
required:
92-
- credentialsRef
9391
- name
92+
- secretRef
9493
type: object
9594
type: array
9695
serverRef:
9796
description: ServerRef is used for configuring Proxmox client
9897
properties:
99-
credentialsRef:
100-
description: CredentialsRef is a reference for secret which contains
98+
endpoint:
99+
description: endpoint is the address of the Proxmox-VE REST API
100+
endpoint.
101+
type: string
102+
secretRef:
103+
description: SecretRef is a reference for secret which contains
101104
proxmox login secrets and ssh configs for proxmox nodes
102105
properties:
103106
name:
@@ -108,15 +111,10 @@ spec:
108111
type: string
109112
required:
110113
- name
111-
- namespace
112114
type: object
113-
endpoint:
114-
description: endpoint is the address of the Proxmox-VE REST API
115-
endpoint.
116-
type: string
117115
required:
118-
- credentialsRef
119116
- endpoint
117+
- secretRef
120118
type: object
121119
storage:
122120
description: storage is for proxmox storage used by vm instances

0 commit comments

Comments
 (0)