Skip to content

Commit

Permalink
bootstrap: Add to bootstrap Loadbalancer and namespace
Browse files Browse the repository at this point in the history
Signed-off-by: Kfir Toledo <[email protected]>
  • Loading branch information
kfirtoledo committed Jun 25, 2024
1 parent 376959f commit f8d37e9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 19 deletions.
10 changes: 5 additions & 5 deletions cmd/clusterlink/cmd/deploy/deploy_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ func (o *PeerOptions) Run() error {
Tag: o.Tag,
}

// Create clusterlink instance YAML for the operator.
if o.IngressPort != apis.DefaultExternalPort { // Set the port config only if it has changed.
platformCfg.IngressPort = o.IngressPort
}

if o.StartInstance == NoStart {
// Create a YAML file for deployment without using the operator.
k8sConfig, err := platform.K8SConfig(platformCfg)
Expand Down Expand Up @@ -260,11 +265,6 @@ func (o *PeerOptions) Run() error {
return err
}

// Create clusterlink instance YAML for the operator.
if o.IngressPort != apis.DefaultExternalPort { // Set the port config only if it has changed.
platformCfg.IngressPort = o.IngressPort
}

instance, err := platform.K8SClusterLinkInstanceConfig(platformCfg, "cl-instance")
if err != nil {
return err
Expand Down
87 changes: 82 additions & 5 deletions pkg/bootstrap/platform/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import (
)

const (
nsTemplate = `---
apiVersion: v1
kind: Namespace
metadata:
name: {{.namespace}}
`
certsTemplate = `---
apiVersion: v1
kind: Secret
Expand Down Expand Up @@ -64,7 +70,6 @@ data:
{{.peerKeyFile}}: {{.peerKey}}
{{.fabricCertFile}}: {{.fabricCert}}
`

k8sTemplate = `---
apiVersion: apps/v1
kind: Deployment
Expand Down Expand Up @@ -237,7 +242,8 @@ roleRef:
subjects:
- kind: ServiceAccount
name: default
namespace: {{.namespace}}`
namespace: {{.namespace}}
`
ClusterLinkInstanceTemplate = `apiVersion: clusterlink.net/v1alpha1
kind: Instance
metadata:
Expand All @@ -263,6 +269,24 @@ spec:
namespace: {{.namespace}}
tag: {{.tag}}
`
ingressTemplate = `---
apiVersion: v1
kind: Service
metadata:
name: clusterlink
namespace: {{.namespace}}
spec:
type: {{.ingressType}}
ports:
- name: dataplane
port: {{.ingressPort }}
targetPort: {{.dataplanePort}}
{{ if .ingressNodePort }}
nodePort: {{.ingressNodePort }}
{{ end }}
selector:
app: {{.dataplaneAppName}}
`
)

// K8SConfig returns a kubernetes deployment file.
Expand Down Expand Up @@ -299,19 +323,36 @@ func K8SConfig(config *Config) ([]byte, error) {
"dataplanePort": dpapi.ListenPort,
}

var k8sConfig bytes.Buffer
t := template.Must(template.New("").Parse(k8sTemplate))
var k8sConfig, nsConfig bytes.Buffer
// ClusterLink namespace
t := template.Must(template.New("").Parse(nsTemplate))
if err := t.Execute(&nsConfig, args); err != nil {
return nil, fmt.Errorf("cannot create K8s namespace from template: %w", err)
}

// ClusterLink components
t = template.Must(template.New("").Parse(k8sTemplate))
if err := t.Execute(&k8sConfig, args); err != nil {
return nil, fmt.Errorf("cannot create k8s configuration from template: %w", err)
}

// ClusterLink certificates
certConfig, err := K8SCertificateConfig(config)
if err != nil {
return nil, err
}

k8sBytes := certConfig
// ClusterLink ingress service
ingressConfig, err := k8SIngressConfig(config)
if err != nil {
return nil, err
}

k8sBytes := nsConfig.Bytes()
k8sBytes = append(k8sBytes, certConfig...)
k8sBytes = append(k8sBytes, k8sConfig.Bytes()...)
k8sBytes = append(k8sBytes, ingressConfig...)

return k8sBytes, nil
}

Expand Down Expand Up @@ -372,6 +413,7 @@ func K8SClusterLinkInstanceConfig(config *Config, name string) ([]byte, error) {
}
args["ingressPort"] = config.IngressPort
}

var clConfig bytes.Buffer
t := template.Must(template.New("").Parse(ClusterLinkInstanceTemplate))
if err := t.Execute(&clConfig, args); err != nil {
Expand Down Expand Up @@ -404,3 +446,38 @@ func K8SEmptyCertificateConfig(config *Config) ([]byte, error) {

return certConfig.Bytes(), nil
}

// k8SIngressConfig returns a kubernetes ingress service.
func k8SIngressConfig(config *Config) ([]byte, error) {
var ingressConfig bytes.Buffer
if config.IngressType == "" {
return ingressConfig.Bytes(), nil
}

args := map[string]interface{}{
"namespace": config.Namespace,
"ingressPort": apis.DefaultExternalPort,
"ingressType": config.IngressType,

"dataplaneAppName": dpapp.Name,
"dataplanePort": dpapi.ListenPort,
}

if config.IngressPort != 0 {
if config.IngressType == string(apis.IngressTypeNodePort) {
args["ingressNodePort"] = config.IngressPort
if (config.IngressPort < 30000) || (config.IngressPort > 32767) {
return nil, fmt.Errorf("nodeport number %v is not in the valid range (30000:32767)", config.IngressPort)
}
} else {
args["ingressPort"] = config.IngressPort
}
}

t := template.Must(template.New("").Parse(ingressTemplate))
if err := t.Execute(&ingressConfig, args); err != nil {
return nil, fmt.Errorf("cannot create K8s namespace from template: %w", err)
}

return ingressConfig.Bytes(), nil
}
15 changes: 7 additions & 8 deletions tests/e2e/k8s/util/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
Expand Down Expand Up @@ -142,13 +143,6 @@ func (f *Fabric) SwitchToNewNamespace(name string, appendName bool) error {
f.baseNamespace = name
}

// create new namespace
for _, p := range f.peers {
if err := p.cluster.CreateNamespace(name); err != nil {
return fmt.Errorf("cannot create namespace %s: %w", name, err)
}
}

if f.namespace != "" {
// delete old namespace
for _, p := range f.peers {
Expand All @@ -165,7 +159,7 @@ func (f *Fabric) SwitchToNewNamespace(name string, appendName bool) error {
}
}

if err := p.cluster.DeleteNamespace(f.namespace); err != nil {
if err := p.cluster.DeleteNamespace(f.namespace); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("cannot delete namespace %s: %w", f.namespace, err)
}
}
Expand All @@ -181,6 +175,11 @@ var deployFunc func(target *peer, cfg *PeerConfig) error
func (f *Fabric) deployUsingOperator(target *peer, cfg *PeerConfig) error {
instanceName := "cl-instance" + f.namespace

// Create namespace to run ClusterLink
if err := target.cluster.CreateNamespace(f.namespace); err != nil {
return fmt.Errorf("cannot create namespace %s: %w", f.namespace, err)
}

// Create ClusterLink instance
instance, err := f.generateClusterlinkInstance(instanceName, target, cfg)
if err != nil {
Expand Down
26 changes: 25 additions & 1 deletion website/content/en/docs/main/tasks/operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The deployment process can be split into two steps:
1. Deploy only ClusterLink operator:

```sh
clusterlink deploy peer ---name <peer_name> --fabric <fabric_name> --start operator
clusterlink deploy peer --name <peer_name> --fabric <fabric_name> --start operator
```

The `start` flag will deploy only the ClusterLink operator and the certificate's secrets as described in the [common use case][] above.
Expand Down Expand Up @@ -121,6 +121,30 @@ The `deploy peer` {{< anchor commandline-flags >}} command has the following fla
- **path**: Represents the path where the peer and fabric certificates are stored,
by default is the working current working directory.
## Manual Deployment without the operator
To deploy the ClusterLink without using the Operator, follow the instructions below:
1. Create a `k8s.yaml` file to deploy ClusterLink without the operator:
```sh
clusterlink deploy peer --name <peer_name> --fabric <fabric_name> --start none
```
The `k8s.yaml` file contains the deployment of all ClusterLink components and can be configured for various purposes, such as adding sidecar pods or managing the ClusterLink certificates.
1. Deploy ClusterLink CRDs:
```sh
curl -L https://github.com/clusterlink-net/clusterlink/archive/refs/heads/main.tar.gz | tar -xzO clusterlink-main/config/crds | kubectl apply -f -
```
1. Apply the `k8s.yaml` file to the cluster:
```sh
kubectl apply ./<fabric_name>/<peer_name>/k8s.yaml
```
## Manual Deployment without CLI
To deploy the ClusterLink without using the CLI, follow the instructions below:
Expand Down

0 comments on commit f8d37e9

Please sign in to comment.