Skip to content

Commit

Permalink
Added loadbalancer support (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
berkayoz authored Feb 6, 2024
1 parent 469aefc commit 1698316
Show file tree
Hide file tree
Showing 31 changed files with 864 additions and 19 deletions.
23 changes: 23 additions & 0 deletions k8s/components/charts/ck-loadbalancer/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions k8s/components/charts/ck-loadbalancer/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: ck-loadbalancer
description: A Helm chart containing LoadBalancer manifests for Canonical Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.1.0"
62 changes: 62 additions & 0 deletions k8s/components/charts/ck-loadbalancer/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "ck-loadbalancer.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "ck-loadbalancer.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "ck-loadbalancer.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "ck-loadbalancer.labels" -}}
helm.sh/chart: {{ include "ck-loadbalancer.chart" . }}
{{ include "ck-loadbalancer.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "ck-loadbalancer.selectorLabels" -}}
app.kubernetes.io/name: {{ include "ck-loadbalancer.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "ck-loadbalancer.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "ck-loadbalancer.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
18 changes: 18 additions & 0 deletions k8s/components/charts/ck-loadbalancer/templates/bgp-policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{- if .Values.bgp.enabled }}
apiVersion: "cilium.io/v2alpha1"
kind: CiliumBGPPeeringPolicy
metadata:
name: {{ include "ck-loadbalancer.fullname" . }}
labels:
{{- include "ck-loadbalancer.labels" . | nindent 4 }}
spec:
virtualRouters:
- localASN: {{ .Values.bgp.localASN }}
serviceSelector:
matchExpressions:
- {key: somekey, operator: NotIn, values: ['never-used-value']}
{{- with .Values.bgp.neighbors }}
neighbors:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end}}
15 changes: 15 additions & 0 deletions k8s/components/charts/ck-loadbalancer/templates/l2-policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{- if .Values.l2.enabled }}
apiVersion: "cilium.io/v2alpha1"
kind: CiliumL2AnnouncementPolicy
metadata:
name: {{ include "ck-loadbalancer.fullname" . }}
labels:
{{- include "ck-loadbalancer.labels" . | nindent 4 }}
spec:
{{- with .Values.l2.interfaces }}
interfaces:
{{- toYaml . | nindent 4 }}
{{- end }}
externalIPs: true
loadBalancerIPs: true
{{- end }}
13 changes: 13 additions & 0 deletions k8s/components/charts/ck-loadbalancer/templates/lb-ip-pool.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{- if .Values.ipPool.cidrs }}
apiVersion: "cilium.io/v2alpha1"
kind: CiliumLoadBalancerIPPool
metadata:
name: {{ include "ck-loadbalancer.fullname" . }}
labels:
{{- include "ck-loadbalancer.labels" . | nindent 4 }}
spec:
{{- with .Values.ipPool.cidrs }}
cidrs:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
75 changes: 75 additions & 0 deletions k8s/components/charts/ck-loadbalancer/values.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"type": "object",
"properties": {
"l2": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
},
"interfaces": {
"type": [ "array", "null" ],
"items": [
{
"type": "string"
}
]
}
}
},
"ipPool": {
"type": "object",
"properties": {
"cidrs": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"cidr": {
"type": "string"
}
},
"required": ["cidr"]
}
]
}
},
"required": ["cidrs"]
},
"bgp": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
},
"localASN": {
"type": "integer"
},
"neighbors": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"peerAddress": {
"type": "string"
},
"peerASN": {
"type": "integer"
},
"peerPort": {
"type": "integer"
}
},
"required": ["peerAddress", "peerASN", "peerPort"]
}
]
}
},
"required": ["localASN", "neighbors"]
}
},
"required": ["l2", "ipPool", "bgp"]
}
20 changes: 20 additions & 0 deletions k8s/components/charts/ck-loadbalancer/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

l2:
enabled: true
# interfaces:
# - "^eth[0-9]+"
interfaces: []

ipPool:
# cidrs:
# - cidr: "10.42.254.176/28"
cidrs: []

bgp:
enabled: false
localASN: 64512
# neighbors:
# - peerAddress: '10.0.0.60/24'
# peerASN: 65100
# peerPort: 179
neighbors: []
4 changes: 4 additions & 0 deletions k8s/components/components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ gateway:
release: "ck-gateway"
chart: "gateway-api-0.7.1.tgz"
namespace: "kube-system"
loadbalancer:
release: "ck-loadbalancer"
chart: "ck-loadbalancer"
namespace: "kube-system"
21 changes: 21 additions & 0 deletions src/k8s/api/v1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ type UpdateGatewayComponentRequest struct {
Status ComponentStatus `json:"status"`
}

// UpdateLoadBalancerComponentRequest is used to update the LoadBalancer component state.
type UpdateLoadBalancerComponentRequest struct {
Status ComponentStatus `json:"status"`
Config LoadBalancerComponentConfig `json:"config,omitempty"`
}

// LoadBalancerComponentConfig holds the configuration values for the LoadBalancer component.
type LoadBalancerComponentConfig struct {
CIDRs []string `json:"cidrs,omitempty"`
L2Enabled bool `json:"l2Enabled,omitempty"`
L2Interfaces []string `json:"l2Interfaces,omitempty"`
BGPEnabled bool `json:"bgpEnabled,omitempty"`
BGPLocalASN int `json:"bgpLocalAsn,omitempty"`
BGPPeerAddress string `json:"bgpPeerAddress,omitempty"`
BGPPeerASN int `json:"bgpPeerAsn,omitempty"`
BGPPeerPort int `json:"bgpPeerPort,omitempty"`
}

// UpdateDNSComponentResponse is the response for "PUT 1.0/k8sd/components/dns".
type UpdateDNSComponentResponse struct{}

Expand All @@ -63,6 +81,9 @@ type UpdateIngressComponentResponse struct{}
// UpdateGatewayComponentResponse is the response for "PUT 1.0/k8sd/components/gateway".
type UpdateGatewayComponentResponse struct{}

// UpdateLoadBalancerComponentResponse is the response for "PUT 1.0/k8sd/components/loadbalancer".
type UpdateLoadBalancerComponentResponse struct{}

// Component holds information about a k8s component.
type Component struct {
Name string `json:"name"`
Expand Down
4 changes: 4 additions & 0 deletions src/k8s/cmd/k8s/k8s_disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ func init() {
rootCmd.AddCommand(disableCmd)
disableCmd.AddCommand(disableDNSCmd)
disableCmd.AddCommand(disableNetworkCmd)
disableCmd.AddCommand(disableStorageCmd)
disableCmd.AddCommand(disableIngressCmd)
disableCmd.AddCommand(disableGatewayCmd)
disableCmd.AddCommand(disableLoadBalancerCmd)
}
3 changes: 1 addition & 2 deletions src/k8s/cmd/k8s/k8s_disable_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ var disableDNSCmd = &cobra.Command{
Status: api.ComponentDisable,
}

err = client.UpdateDNSComponent(cmd.Context(), request)
if err != nil {
if err := client.UpdateDNSComponent(cmd.Context(), request); err != nil {
return fmt.Errorf("failed to disable DNS component: %w", err)
}

Expand Down
35 changes: 35 additions & 0 deletions src/k8s/cmd/k8s/k8s_disable_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package k8s

import (
"fmt"

api "github.com/canonical/k8s/api/v1"
"github.com/canonical/k8s/pkg/k8s/client"
"github.com/spf13/cobra"
)

var disableGatewayCmd = &cobra.Command{
Use: "gateway",
Short: "Disable the Gateway component in the cluster.",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := client.NewClient(cmd.Context(), client.ClusterOpts{
StateDir: clusterCmdOpts.stateDir,
Verbose: rootCmdOpts.logVerbose,
Debug: rootCmdOpts.logDebug,
})
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}

request := api.UpdateGatewayComponentRequest{
Status: api.ComponentDisable,
}

if err := client.UpdateGatewayComponent(cmd.Context(), request); err != nil {
return fmt.Errorf("failed to disable Gateway component: %w", err)
}

cmd.Println("Component 'Gateway' disabled")
return nil
},
}
35 changes: 35 additions & 0 deletions src/k8s/cmd/k8s/k8s_disable_ingress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package k8s

import (
"fmt"

api "github.com/canonical/k8s/api/v1"
"github.com/canonical/k8s/pkg/k8s/client"
"github.com/spf13/cobra"
)

var disableIngressCmd = &cobra.Command{
Use: "ingress",
Short: "Disable the Ingress component in the cluster.",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := client.NewClient(cmd.Context(), client.ClusterOpts{
StateDir: clusterCmdOpts.stateDir,
Verbose: rootCmdOpts.logVerbose,
Debug: rootCmdOpts.logDebug,
})
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}

request := api.UpdateIngressComponentRequest{
Status: api.ComponentDisable,
}

if err := client.UpdateIngressComponent(cmd.Context(), request); err != nil {
return fmt.Errorf("failed to disable Ingress component: %w", err)
}

cmd.Println("Component 'Ingress' disabled")
return nil
},
}
Loading

0 comments on commit 1698316

Please sign in to comment.