Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 408d411

Browse files
committed
save CA as secret
1 parent 3b0a175 commit 408d411

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+405
-10909
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ endif
122122
$(GO) run -mod=vendor vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go all
123123
# patch the particular image tag we will want to deploy
124124
@echo "updating kustomize image patch file for manager resource"
125-
sed -i'' -e 's@PATCH_ME_IMAGE@image: '"$(PATCH_IMAGE_TAG)"'@' ./config/default/manager_image_patch.yaml
125+
sed -i'' -e 's@PATCH_ME_IMAGE@'"$(PATCH_IMAGE_TAG)"'@' ./config/default/manager_image_patch.yaml
126126
# create the manifests
127127
$(KUBECTL) kustomize vendor/sigs.k8s.io/cluster-api/config/default/ > $(PROVIDERYAML)
128128
echo "---" >> $(PROVIDERYAML)

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To use the cluster-api to deploy a Kubernetes cluster to Packet, you need the fo
1111
* A Packet API key
1212
* A Packet project ID
1313
* The `clusterctl` binary from this repository.
14-
* A Kubernetes cluster - the "bootstrap cluster" - that will deploy and manage the cluster on Packet.
14+
* A Kubernetes cluster - the "bootstrap cluster" - that will deploy and manage the cluster on Packet.
1515
* `kubectl` - not absolutely required, but hard to interact with a cluster without it
1616

1717
For the bootstrap cluster, any cluster is just fine for this, including [k3s](https://k3s.io), [k3d](https://github.com/rancher/k3d) and [kind](https://github.com/kubernetes-sigs/kind).
@@ -34,13 +34,15 @@ To deploy a cluster:
3434
* `CLUSTER_NAME` - The created cluster will have this name. If not set, it will generate one for you.
3535
* `FACILITY` - The Packet facility where you wantto deploy the cluster. If not set, it will default to `ewr1`.
3636
* `SSH_KEY` - The path to an ssh public key to place on all of the machines. If not set, it will use whichever ssh keys are defined for your project.
37+
* `CA_KEY` - The path to a file with the CA private key. If not set, it will generate one for you.
38+
* `CA_CERT` - The path to a file with the CA certificate. If not set, it will generate one for you.
3739
1. Create the config files you need via `./generate-yaml.sh`. This will generate the following files in [out/packet](./out/packet):
3840
* `cluster.yaml`
3941
* `machines.yaml`
40-
* `provider-components.yaml` - note that this file _will_ contain your secrets, specifically `PACKET_API_KEY`, to be loaded into the cluster
42+
* `provider-components.yaml` - note that this file _will_ contain your secrets, specifically `PACKET_API_KEY`, to be loaded into the cluster, and optionally your CA private key, if provided (but not if auto-generated)
4143
* `addons.yaml` - note that this file _will_ contain your secrets, specifically `PACKET_API_KEY`, to be loaded into the cluster
4244
1. If desired, edit the following files:
43-
* `cluster.yaml` - to change parameters or settings, including network CIDRs, and, if desired, your own CA certificate and key
45+
* `cluster.yaml` - to change parameters or settings, including network CIDRs
4446
* `machines.yaml` - to change parameters or settings, including machine types and quantity
4547
1. Run `clusterctl` with the appropriate command.
4648

@@ -72,7 +74,6 @@ Run `clusterctl create cluster --help` for more options, for example to use an e
7274

7375
If you do not change the generated `yaml` files, it will use defaults. You can look in the `*.yaml.template` files in [cmd/clusterctl/examples/packet/](./cmd/clusterctl/examples/packet/) for details.
7476

75-
* CA key/certificate: leave blank, which will cause the `manager` to create one.
7677
* service CIDR: `172.25.0.0/16`
7778
* pod CIDR: `172.26.0.0/16`
7879
* service domain: `cluster.local`
@@ -112,7 +113,7 @@ Note that, unlike `clusterctl`, this method will not take care of the following:
112113

113114
The components deployed via the `yaml` files are the following:
114115

115-
* `cluster.yaml` - contains
116+
* `cluster.yaml` - contains
116117
* a single `Cluster` CRD which defines the new cluster to be deployed. Includes cluster-wide definitions, including cidr definitions for services and pods.
117118
* `machines.yaml` - contains
118119
* one or more `Machine` CRDs, which cause the deployment of individual server instance to serve as Kubernetes master or worker nodes.
@@ -142,9 +143,9 @@ The Packet cluster-api provider follows the standard design for cluster-api. It
142143
The actual machines are deployed using `kubeadm`. The deployment process uses the following process.
143144

144145
1. When a new `Cluster` is created:
145-
* if the `ClusterSpec` does not include a CA key/certificate pair, create one and save it on the `Cluster` object
146+
* if the appropriate `Secret` does not include a CA key/certificate pair, create one and save it in that `Secret`
146147
2. When a new master `Machine` is created:
147-
* retrieve the CA certificate and key from the `Cluster` object
148+
* retrieve the CA certificate and key from the appropriate Kubernetes `Secret`
148149
* launch a new server instance on Packet
149150
* set the `cloud-init` on the instance to run `kubeadm init`, passing it the CA certificate and key
150151
3. When a new worker `Machine` is created:
@@ -265,5 +266,3 @@ Important notes:
265266
## References
266267

267268
* [kubeadm yaml api](https://godoc.org/k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2)
268-
269-

cmd/clusterctl/main.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ import (
2222
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet"
2323
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/deployer"
2424
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/util"
25+
kclient "k8s.io/client-go/kubernetes"
26+
clientv1 "k8s.io/client-go/kubernetes/typed/core/v1"
2527
"k8s.io/klog"
2628
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd"
2729
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
30+
"sigs.k8s.io/controller-runtime/pkg/client/config"
2831
)
2932

3033
func main() {
31-
var err error
34+
var (
35+
err error
36+
kube *kclient.Clientset
37+
secretsGetter clientv1.SecretsGetter
38+
)
3239

3340
flag.Parse()
3441

@@ -38,10 +45,19 @@ func main() {
3845
klog.Fatalf("unable to get Packet client: %v", err)
3946
}
4047

48+
cfg, _ := config.GetConfig()
49+
if cfg != nil {
50+
kube, _ = kclient.NewForConfig(cfg)
51+
}
52+
if kube != nil {
53+
secretsGetter = kube.CoreV1()
54+
}
55+
4156
// get a deployer, which is needed at various stages
4257
deployer, err := deployer.New(deployer.Params{
43-
Port: util.ControlPort,
44-
Client: client,
58+
Port: util.ControlPort,
59+
SecretsGetter: secretsGetter,
60+
Client: client,
4561
})
4662
if err != nil {
4763
klog.Fatalf("unable to get deployer: %v", err)

cmd/manager/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/actuators/machine/machineconfig"
2828
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/deployer"
2929
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/util"
30+
kclient "k8s.io/client-go/kubernetes"
3031
"k8s.io/klog"
3132
clusterapis "sigs.k8s.io/cluster-api/pkg/apis"
3233
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
@@ -67,6 +68,10 @@ func main() {
6768
if err != nil {
6869
klog.Fatalf(err.Error())
6970
}
71+
kube, err := kclient.NewForConfig(cfg)
72+
if err != nil {
73+
klog.Fatalf(err.Error())
74+
}
7075

7176
// get a packet client
7277
client, err := packet.GetClient()
@@ -75,8 +80,9 @@ func main() {
7580
}
7681
// get a deployer, which is needed at various stages
7782
deployer, err := deployer.New(deployer.Params{
78-
Client: client,
79-
Port: util.ControlPort,
83+
Client: client,
84+
SecretsGetter: kube.CoreV1(),
85+
Port: util.ControlPort,
8086
})
8187
if err != nil {
8288
klog.Fatalf(err.Error())
@@ -85,6 +91,7 @@ func main() {
8591
clusterInterface := cs.ClusterV1alpha1()
8692
clusterActuator, err := cluster.NewActuator(cluster.ActuatorParams{
8793
ClustersGetter: clusterInterface,
94+
SecretsGetter: kube.CoreV1(),
8895
Deployer: deployer,
8996
})
9097
if err != nil {
@@ -100,6 +107,7 @@ func main() {
100107
machineActuator, err := machine.NewActuator(machine.ActuatorParams{
101108
MachinesGetter: clusterInterface,
102109
MachineConfigGetter: getter,
110+
SecretsGetter: kube.CoreV1(),
103111
Client: client,
104112
Deployer: deployer,
105113
})

config/crds/packetprovider_v1alpha1_packetclusterproviderspec.yaml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@ spec:
1919
of an object. Servers should convert recognized schemas to the latest
2020
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
2121
type: string
22-
caKeyPair:
23-
description: CAKeyPair is the key pair for ca certs.
24-
properties:
25-
cert:
26-
description: base64 encoded cert and key
27-
format: byte
28-
type: string
29-
key:
30-
format: byte
31-
type: string
32-
required:
33-
- cert
34-
- key
35-
type: object
3622
kind:
3723
description: 'Kind is a string value representing the REST resource this
3824
object represents. Servers may infer this from the endpoint the client

config/kustomization.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Adds namespace to all resources.
2+
# This must match PRECISELY what is in the constant in pkg/cloud/packet/util.CAPPNamespace
23
namespace: cluster-api-provider-packet-system
34

45
# Value of this field is prepended to the
56
# names of all resources, e.g. a deployment named
67
# "wordpress" becomes "alices-wordpress".
78
# Note that it should also match with the prefix (text before '-') of the namespace
89
# field above.
10+
# This must match PRECISELY what is in the constant in pkg/cloud/packet/util.CAPPPrefix
911
namePrefix: cluster-api-provider-packet-
1012

1113
bases:

config/manager/manager.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,13 @@ stringData:
103103
apiKey: $PACKET_API_KEY
104104
projectID: $PACKET_PROJECT_ID
105105
type: Opaque
106+
---
107+
apiVersion: v1
108+
kind: Secret
109+
metadata:
110+
name: ca-$CLUSTER_NAME
111+
namespace: system
112+
stringData:
113+
key: $CA_KEY
114+
certificate: $CA_CERT
115+
type: Opaque

generate-yaml.sh

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ if [ -z "$PACKET_API_KEY" ]; then
126126
exit 1
127127
fi
128128

129-
130129
mkdir -p ${OUTPUT_DIR}
131130

132131
SSH_KEY=${SSH_KEY:-}
@@ -141,13 +140,42 @@ else
141140
SSH_KEY=$SSH_PUBLIC_FILE
142141
fi
143142

143+
CA_KEY="${CA_KEY:-}"
144+
CA_CERT="${CA_CERT:-}"
145+
CA_KEY_CONTENT=
146+
CA_CERT_CONTENT=
147+
if [ -z "$CA_KEY" -o -z "$CA_CERT" ]; then
148+
echo "CA key and CA certificate not provided, will generate automatically"
149+
elif [ ! -e "$CA_KEY" ]; then
150+
echo "CA private key file $CA_KEY does not exist" >&2
151+
exit 1
152+
elif [ ! -e "$CA_CERT" ]; then
153+
echo "CA certificate file $CA_CERT does not exist" >&2
154+
exit 1
155+
else
156+
CA_KEY_CONTENT=$(cat $CA_KEY | base64 | tr -d '\r\n')
157+
CA_CERT_CONTENT=$(cat $CA_CERT | base64 | tr -d '\r\n')
158+
fi
159+
# to be sane about the output
160+
if [ -z "$CA_KEY_CONTENT" ]; then
161+
CA_KEY_CONTENT="''"
162+
fi
163+
if [ -z "$CA_CERT_CONTENT" ]; then
164+
CA_CERT_CONTENT="''"
165+
fi
166+
167+
168+
144169
# By default, linux wraps base64 output every 76 cols, so we use 'tr -d' to remove whitespaces.
145170
# Note 'base64 -w0' doesn't work on Mac OS X, which has different flags.
146171
SSH_PUBLIC=$(cat $SSH_KEY | base64 | tr -d '\r\n')
147172

148173
cat $PROVIDER_TEMPLATE_FILE \
174+
| sed -e "s/\$CLUSTER_NAME/$CLUSTER_NAME/" \
149175
| sed -e "s/\$PACKET_PROJECT_ID/$PACKET_PROJECT_ID/" \
150-
| sed -e "s/\$PACKET_API_KEY/$PACKET_API_KEY/" > $PROVIDER_GENERATED_FILE
176+
| sed -e "s/\$PACKET_API_KEY/$PACKET_API_KEY/" \
177+
| sed -e "s/\$CA_KEY/$CA_KEY_CONTENT/" \
178+
| sed -e "s/\$CA_CERT/$CA_CERT_CONTENT/" > $PROVIDER_GENERATED_FILE
151179

152180
cat $MACHINE_TEMPLATE_FILE \
153181
| sed -e "s/\$CLUSTER_NAME/$CLUSTER_NAME/" \

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ require (
1818
go.uber.org/zap v1.10.0 // indirect
1919
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980
2020
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
21-
2221
k8s.io/api v0.0.0-20190222213804-5cb15d344471
2322
k8s.io/apimachinery v0.0.0-20190703205208-4cfb76a8bf76
2423
k8s.io/client-go v10.0.0+incompatible

pkg/apis/packetprovider/v1alpha1/packetclusterproviderspec_types.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ type PacketClusterProviderSpec struct {
3333
metav1.ObjectMeta `json:"metadata,omitempty"`
3434

3535
ProjectID string `json:"projectID"`
36-
37-
// CAKeyPair is the key pair for ca certs.
38-
CAKeyPair KeyPair `json:"caKeyPair,omitempty"`
39-
}
40-
41-
// KeyPair is how operators can supply custom keypairs for kubeadm to use.
42-
type KeyPair struct {
43-
// base64 encoded cert and key
44-
Cert []byte `json:"cert"`
45-
Key []byte `json:"key"`
4636
}
4737

4838
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

0 commit comments

Comments
 (0)