Skip to content

Commit

Permalink
WIP. Issue #54
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Oct 23, 2023
1 parent 1859b59 commit 7452b6a
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 83 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ This can be useful in several ways:

`./idpbuilder create --buildName localdev`

You can also define the kubernetes version to image and which corresponds to the kind pre-built [image](https://github.com/kubernetes-sigs/kind/releases).
`./idpbuilder create --kubeVersion v1.27.3`

If it is needed to expose some extra Ports between the docker container and the kubernetes host, they can be declared as such
`./idpbuilder create --extraPorts 22:32222`

### Use

Kubernetes: `kubectl get pods`
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/docker/go-connections v0.4.0
github.com/google/go-cmp v0.5.9
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
k8s.io/api v0.26.2
k8s.io/apiextensions-apiserver v0.24.2
k8s.io/apimachinery v0.26.2
Expand Down Expand Up @@ -103,6 +104,7 @@ require (
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
Expand Down
8 changes: 0 additions & 8 deletions pkg/apps/srv/nginx-ingress/ingress-nginx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -501,17 +501,9 @@ spec:
readOnly: true
dnsPolicy: ClusterFirst
nodeSelector:
ingress-ready: "true"
kubernetes.io/os: linux
serviceAccountName: ingress-nginx
terminationGracePeriodSeconds: 0
tolerations:
- effect: NoSchedule
key: node-role.kubernetes.io/master
operator: Equal
- effect: NoSchedule
key: node-role.kubernetes.io/control-plane
operator: Equal
volumes:
- name: webhook-cert
secret:
Expand Down
24 changes: 14 additions & 10 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,28 @@ var (
)

type Build struct {
name string
kubeConfigPath string
scheme *runtime.Scheme
CancelFunc context.CancelFunc
name string
kubeConfigPath string
kubeVersion string
extraPortsMapping string
scheme *runtime.Scheme
CancelFunc context.CancelFunc
}

func NewBuild(name, kubeConfigPath string, scheme *runtime.Scheme, ctxCancel context.CancelFunc) *Build {
func NewBuild(name, kubeVersion, kubeConfigPath, extraPortsMapping string, scheme *runtime.Scheme, ctxCancel context.CancelFunc) *Build {
return &Build{
name: name,
kubeConfigPath: kubeConfigPath,
scheme: scheme,
CancelFunc: ctxCancel,
name: name,
kubeConfigPath: kubeConfigPath,
kubeVersion: kubeVersion,
extraPortsMapping: extraPortsMapping,
scheme: scheme,
CancelFunc: ctxCancel,
}
}

func (b *Build) ReconcileKindCluster(ctx context.Context, recreateCluster bool) error {
// Initialize Kind Cluster
cluster, err := kind.NewCluster(b.name, b.kubeConfigPath)
cluster, err := kind.NewCluster(b.name, b.kubeVersion, b.kubeConfigPath, b.extraPortsMapping)
if err != nil {
setupLog.Error(err, "Error Creating kind cluster")
return err
Expand Down
14 changes: 9 additions & 5 deletions pkg/cmd/create/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ import (

var (
// Flags
recreateCluster bool
buildName string
recreateCluster bool
buildName string
kubeVersion string
extraPortsMapping string
)

var CreateCmd = &cobra.Command{
Use: "create",
Short: "(Re)Create a UCP cluster",
Short: "(Re)Create an IDP cluster",
Long: ``,
RunE: create,
}

func init() {
CreateCmd.PersistentFlags().BoolVar(&recreateCluster, "recreate", false, "Delete cluster first if it already exists.")
CreateCmd.PersistentFlags().StringVar(&buildName, "buildName", "localdev", "Name for build (Prefix for kind cluster name, pod names, etc)")
CreateCmd.PersistentFlags().StringVar(&buildName, "buildName", "localdev", "Name for build (Prefix for kind cluster name, pod names, etc).")
CreateCmd.PersistentFlags().StringVar(&kubeVersion, "kubeVersion", "v1.26.333", "Version of the kind kubernetes cluster to create.")
CreateCmd.PersistentFlags().StringVar(&extraPortsMapping, "extraPorts", "", "List of extra ports to expose on the docker container and kubernetes cluster as nodePort (e.g. \"22:32222,9090:39090,etc\").")

zapfs := flag.NewFlagSet("zap", flag.ExitOnError)
opts := zap.Options{
Expand All @@ -54,7 +58,7 @@ func create(cmd *cobra.Command, args []string) error {
os.Exit(1)
}

b := build.NewBuild(buildName, kubeConfigPath, k8s.GetScheme(), ctxCancel)
b := build.NewBuild(buildName, kubeVersion, kubeConfigPath, extraPortsMapping, k8s.GetScheme(), ctxCancel)

if err := b.Run(ctx, recreateCluster); err != nil {
return err
Expand Down
48 changes: 41 additions & 7 deletions pkg/kind/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,54 @@ import (
"embed"
"fmt"
"io/fs"
"strings"
"text/template"

"sigs.k8s.io/kind/pkg/cluster"
)

type Cluster struct {
provider *cluster.Provider
name string
kubeConfigPath string
provider *cluster.Provider
name string
kubeVersion string
kubeConfigPath string
extraPortsMapping string
}

type PortMapping struct {
HostPort string
ContainerPort string
}

//go:embed resources/kind.yaml
var configFS embed.FS

func SplitFunc(input, sep string) []string {
return strings.Split(input, sep)
}
func (c *Cluster) getConfig() ([]byte, error) {
rawConfigTempl, err := fs.ReadFile(configFS, "resources/kind.yaml")
if err != nil {
return []byte{}, err
}

var portMappingPairs []PortMapping
if len(c.extraPortsMapping) > 0 {
// Split pairs of ports "11=1111","22=2222",etc
pairs := strings.Split(c.extraPortsMapping, ",")
// Create a slice to store PortMapping pairs.
portMappingPairs = make([]PortMapping, len(pairs))
// Parse each pair into PortPair objects.
for i, pair := range pairs {
parts := strings.Split(pair, ":")
if len(parts) == 2 {
portMappingPairs[i] = PortMapping{parts[0], parts[1]}
}
}
} else {
portMappingPairs = nil
}

template, err := template.New("kind.yaml").Parse(string(rawConfigTempl))
if err != nil {
return []byte{}, err
Expand All @@ -36,23 +64,29 @@ func (c *Cluster) getConfig() ([]byte, error) {
RegistryHostname string
ExposedRegistryPort uint16
InternalRegistryPort uint16
KubernetesVersion string
ExtraPortsMapping []PortMapping
}{
RegistryHostname: c.getRegistryContainerName(),
ExposedRegistryPort: ExposedRegistryPort,
InternalRegistryPort: InternalRegistryPort,
KubernetesVersion: c.kubeVersion,
ExtraPortsMapping: portMappingPairs,
}); err != nil {
return []byte{}, err
}
return retBuff.Bytes(), nil
}

func NewCluster(name string, kubeConfigPath string) (*Cluster, error) {
func NewCluster(name, kubeVersion, kubeConfigPath, extraPortsMapping string) (*Cluster, error) {
provider := cluster.NewProvider(cluster.ProviderWithDocker())

return &Cluster{
provider: provider,
name: name,
kubeConfigPath: kubeConfigPath,
provider: provider,
name: name,
kubeVersion: kubeVersion,
kubeConfigPath: kubeConfigPath,
extraPortsMapping: extraPortsMapping,
}, nil
}

Expand Down
110 changes: 69 additions & 41 deletions pkg/kind/cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package kind

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestGetConfig(t *testing.T) {
cluster, err := NewCluster("testcase", "")
cluster, err := NewCluster("testcase", "v1.26.3", "", "")
if err != nil {
t.Fatalf("Initializing cluster resource: %v", err)
}
Expand All @@ -17,43 +16,72 @@ func TestGetConfig(t *testing.T) {
t.Errorf("Error getting kind config: %v", err)
}

expectConfig := `# two node (one workers) cluster config
# Kind kubernetes release images https://github.com/kubernetes-sigs/kind/releases
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5000"]
endpoint = ["http://idpbuilder-registry:5000"]
nodes:
- role: control-plane
image: "kindest/node:v1.25.3@sha256:f52781bc0d7a19fb6c405c2af83abfeb311f130707a0e219175677e366cc45d1"
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
system-reserved: memory=4Gi
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 8880
protocol: TCP
- containerPort: 443
hostPort: 8443
protocol: TCP
-
- role: worker
image: "kindest/node:v1.25.3@sha256:f52781bc0d7a19fb6c405c2af83abfeb311f130707a0e219175677e366cc45d1"
kubeadmConfigPatches:
- |
kind: JoinConfiguration
nodeRegistration:
kubeletExtraArgs:
system-reserved: memory=4Gi`

t.Errorf("Got config: %s", string(cfg))
if diff := cmp.Diff(expectConfig, string(cfg)); diff != "" {
t.Errorf("Expected config mismatch (-want +got):\n%s", diff)
expectConfig := `# Kind kubernetes release images https://github.com/kubernetes-sigs/kind/releases
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5001"]
endpoint = ["http://idpbuilder-testcase-registry:5000"]
nodes:
- role: control-plane
image: "kindest/node:v1.26.3"
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
system-reserved: memory=4Gi
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 8880
protocol: TCP
- containerPort: 443
hostPort: 8443
protocol: TCP
`
assert.Equal(t, expectConfig, string(cfg))
}

func TestExtraPortMappings(t *testing.T) {
cluster, err := NewCluster("testcase", "v1.26.3", "", "22:32222")
if err != nil {
t.Fatalf("Initializing cluster resource: %v", err)
}

cfg, err := cluster.getConfig()
if err != nil {
t.Errorf("Error getting kind config: %v", err)
}

expectConfig := `# Kind kubernetes release images https://github.com/kubernetes-sigs/kind/releases
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5001"]
endpoint = ["http://idpbuilder-testcase-registry:5000"]
nodes:
- role: control-plane
image: "kindest/node:v1.26.3"
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
system-reserved: memory=4Gi
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 8880
protocol: TCP
- containerPort: 443
hostPort: 8443
protocol: TCP
- containerPort: 32222
hostPort: 22
protocol: TCP`

assert.Equal(t, expectConfig, string(cfg))
}
2 changes: 1 addition & 1 deletion pkg/kind/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestReconcileRegistry(t *testing.T) {
defer dockerCli.Close()

// Create cluster
cluster, err := NewCluster("testcase", "")
cluster, err := NewCluster("testcase", "v1.26.3", "", "")
if err != nil {
t.Fatalf("Initializing cluster resource: %v", err)
}
Expand Down
15 changes: 4 additions & 11 deletions pkg/kind/resources/kind.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# two node (one workers) cluster config
# Kind kubernetes release images https://github.com/kubernetes-sigs/kind/releases
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
Expand All @@ -8,7 +7,7 @@ containerdConfigPatches:
endpoint = ["http://{{ .RegistryHostname }}:{{ .InternalRegistryPort }}"]
nodes:
- role: control-plane
image: "kindest/node:v1.26.3"
image: "kindest/node:{{ .KubernetesVersion }}"
kubeadmConfigPatches:
- |
kind: InitConfiguration
Expand All @@ -23,12 +22,6 @@ nodes:
- containerPort: 443
hostPort: 8443
protocol: TCP
-
- role: worker
image: "kindest/node:v1.26.3"
kubeadmConfigPatches:
- |
kind: JoinConfiguration
nodeRegistration:
kubeletExtraArgs:
system-reserved: memory=4Gi
{{ range .ExtraPortsMapping }}- containerPort: {{ .ContainerPort }}
hostPort: {{ .HostPort }}
protocol: TCP{{ end }}

0 comments on commit 7452b6a

Please sign in to comment.