Skip to content

Commit

Permalink
Added support for gradual rollout
Browse files Browse the repository at this point in the history
Signed-off-by: Patryk Strusiewicz-Surmacki <[email protected]>
  • Loading branch information
p-strusiewiczsurmacki-mobica committed Aug 19, 2024
1 parent 9281253 commit bb8c516
Show file tree
Hide file tree
Showing 51 changed files with 3,146 additions and 469 deletions.
19 changes: 5 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM docker.io/library/golang:1.21-alpine as builder
FROM docker.io/library/golang:1.21-alpine AS builder


WORKDIR /workspace
Expand All @@ -10,28 +10,19 @@ COPY go.sum go.sum
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Build router
RUN apk add llvm clang linux-headers libbpf-dev musl-dev

# Copy the go source
COPY cmd/manager/main.go main.go
COPY cmd/operator/main.go main.go
COPY api/ api/
COPY controllers/ controllers/
COPY pkg/ pkg/

# Build router
COPY bpf/ bpf/
RUN cd pkg/bpf/ && go generate

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o operator main.go

FROM alpine:latest

RUN apk add --no-cache iptables ip6tables

WORKDIR /
COPY --from=builder /workspace/manager .
COPY --from=builder /workspace/operator .
USER 65532:65532

ENTRYPOINT ["/manager"]
ENTRYPOINT ["/operator"]
49 changes: 39 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

# Image URL to use all building/pushing image targets
IMG ?= ghcr.io/telekom/das-schiff-network-operator:latest
# Agent image URL to use all building/pushing image targets
AGENT_IMG ?= ghcr.io/telekom/das-schiff-network-operator-agent:latest
# Sidecar image URL to use all building/pushing image targets
SIDECAR_IMG ?= ghcr.io/telekom/frr-exporter:latest
# Operator image URL to use all building/pushing image targets
OPERATOR_IMG ?= ghcr.io/telekom/das-schiff-network-opeator:latest
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.25

Expand Down Expand Up @@ -68,33 +70,59 @@ test: manifests generate fmt vet envtest ## Run tests.
##@ Build

.PHONY: build
build: generate fmt vet ## Build manager binary.
go build -o bin/manager cmd/manager/main.go
build: generate fmt vet ## Build agent binary.
go build -o bin/operator cmd/operator/main.go
go build -o bin/agent cmd/agent/main.go
go build -o bin/frr-exporter cmd/frr-exporter/main.go

.PHONY: operator-build
operator-build: generate fmt vet ## Build agent binary.
go build -o bin/operator cmd/operator/main.go

.PHONY: agent-build
agent-build: generate fmt vet ## Build agent binary.
go build -o bin/agent cmd/agent/main.go

.PHONY: sidecar-build
sidecar-build: build
go build -o bin/frr-exporter cmd/frr-exporter/main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/manager/main.go
go run ./cmd/agent/main.go

.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} .
docker build -t ${OPERATOR_IMG} .
docker build -t ${AGENT_IMG} -f agent.Dockerfile .
docker build -t ${SIDECAR_IMG} -f frr-exporter.Dockerfile .

.PHONY: docker-build-agent
docker-build-agent: test ## Build docker image with the manager.
docker build -t ${AGENT_IMG} -f agent.Dockerfile .

.PHONY: docker-build-sidecar
docker-build-sidecar: test ## Build docker image with the manager.
docker build -t ${SIDECAR_IMG} -f frr-exporter.Dockerfile .

.PHONY: docker-build-operator
docker-build-operator: test ## Build docker image with the manager.
docker build -t ${OPERATOR_IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker push ${IMG}
docker-push: docker-push-agent docker-push-sidecar docker-push-operator

.PHONY: docker-push-agent
docker-push-agent: ## Push docker image with the manager.
docker push ${AGENT_IMG}

.PHONY: docker-push-sidecar
docker-push-sidecar: ## Push docker image with the manager.
docker push ${SIDECAR_IMG}

.PHONY: docker-push-operator
docker-push-operator: ## Push docker image with the manager.
docker push ${OPERATOR_IMG}

##@ Release

Expand Down Expand Up @@ -133,8 +161,9 @@ uninstall-certs: manifests kustomize ## Uninstall certs

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
cd config/manager && $(KUSTOMIZE) edit set image frr-exporter=${SIDECAR_IMG}
cd config/agent && $(KUSTOMIZE) edit set image agent=${AGENT_IMG}
cd config/agent && $(KUSTOMIZE) edit set image frr-exporter=${SIDECAR_IMG}
cd config/operator && $(KUSTOMIZE) edit set image operator=${OPERATOR_IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -

.PHONY: undeploy
Expand Down
37 changes: 37 additions & 0 deletions agent.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Build the manager binary
FROM docker.io/library/golang:1.21-alpine AS builder


WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Build router
RUN apk add llvm clang linux-headers libbpf-dev musl-dev

# Copy the go source
COPY cmd/agent/main.go main.go
COPY api/ api/
COPY controllers/ controllers/
COPY pkg/ pkg/

# Build router
COPY bpf/ bpf/
RUN cd pkg/bpf/ && go generate

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o agent main.go

FROM alpine:latest

RUN apk add --no-cache iptables ip6tables

WORKDIR /
COPY --from=builder /workspace/agent .
USER 65532:65532

ENTRYPOINT ["/agent"]
102 changes: 102 additions & 0 deletions api/v1alpha1/networkconfigrevision_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// NetworkConfigSpec defines the desired state of NetworkConfig.
type NetworkConfigRevisionSpec struct {
// Config stores global configuration of the nodes.
Config NodeNetworkConfigSpec `json:"config"`
// Revision is a hash of the NetworkConfigRevision object that is used to identify the particular revision.
Revision string `json:"revision"`
}

type NetworkConfigRevisionStatus struct {
// IsInvalid determines if NetworkConfigRevision results in misconfigured nodes (invalid configuration).
IsInvalid bool `json:"isInvalid"`
// Ready informs about how many nodes were already provisioned with a config derived from the revision.
Ready int `json:"ready"`
// Ongoing informs about how many nodes are currently provisioned with a config derived from the revision.
Ongoing int `json:"ongoing"`
// Queued informs about how many nodes are currently waiting to be provisiined with a config derived from the revision.
Queued int `json:"queued"`
// Total informs about how many nodes in total can be provisiined with a config derived from the revision.
Total int `json:"total"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:shortName=ncr,scope=Cluster
//+kubebuilder:printcolumn:name="Invalid",type=string,JSONPath=`.status.isInvalid`
//+kubebuilder:printcolumn:name="Queued",type="integer",JSONPath=".status.queued"
//+kubebuilder:printcolumn:name="Ongoing",type="integer",JSONPath=".status.ongoing"
//+kubebuilder:printcolumn:name="Ready",type="integer",JSONPath=".status.ready"
//+kubebuilder:printcolumn:name="Total",type="integer",JSONPath=".status.total"
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"

// NetworkConfigRevision is the Schema for the node configuration.
type NetworkConfigRevision struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec NetworkConfigRevisionSpec `json:"spec,omitempty"`
Status NetworkConfigRevisionStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// NetworkConfigRevisionList contains a list of NetworkConfigRevision.
type NetworkConfigRevisionList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []NetworkConfigRevision `json:"items"`
}

func NewRevision(config *NodeNetworkConfig) (*NetworkConfigRevision, error) {
data, err := json.Marshal(config.Spec)
if err != nil {
return nil, fmt.Errorf("error marshalling data: %w", err)
}

h := sha256.New()
if _, err := h.Write(data); err != nil {
return nil, fmt.Errorf("error writing MD5 data: %w", err)
}
hash := h.Sum(nil)
hashHex := hex.EncodeToString(hash)

return &NetworkConfigRevision{
ObjectMeta: metav1.ObjectMeta{Name: hashHex[:10]},
Spec: NetworkConfigRevisionSpec{
Config: config.Spec,
Revision: hashHex,
},
Status: NetworkConfigRevisionStatus{},
}, nil
}

func init() {
SchemeBuilder.Register(&NetworkConfigRevision{}, &NetworkConfigRevisionList{})
}
86 changes: 86 additions & 0 deletions api/v1alpha1/nodenetworkconfig_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"reflect"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// NodeNetworkConfigSpec defines the desired state of NodeConfig.
type NodeNetworkConfigSpec struct {
// Revision stores hash of the NodeConfigRevision that was used to create the NodeNetwokrConfig obejct.
Revision string `json:"revision"`
Layer2 []Layer2NetworkConfigurationSpec `json:"layer2"`
Vrf []VRFRouteConfigurationSpec `json:"vrf"`
RoutingTable []RoutingTableSpec `json:"routingTable"`
}

// NodeNetworkConfigStatus defines the observed state of NodeConfig.
type NodeNetworkConfigStatus struct {
// ConfigStatus describes provisioning state od the NodeConfig. Can be either 'provisioning' or 'provisioned'.
ConfigStatus string `json:"configStatus"`
// LastUpdate determines when last update (change) of the ConfigStatus field took place.
LastUpdate metav1.Time `json:"lastUpdate"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:shortName=nnc,scope=Cluster
//+kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.configStatus`
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"

// NodeNetworkConfig is the Schema for the node configuration.
type NodeNetworkConfig struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec NodeNetworkConfigSpec `json:"spec,omitempty"`
Status NodeNetworkConfigStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// NodeNetworkConfigList contains a list of NodeConfig.
type NodeNetworkConfigList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []NodeNetworkConfig `json:"items"`
}

func (nc *NodeNetworkConfig) IsEqual(c *NodeNetworkConfig) bool {
return reflect.DeepEqual(nc.Spec.Layer2, c.Spec.Layer2) && reflect.DeepEqual(nc.Spec.Vrf, c.Spec.Vrf) && reflect.DeepEqual(nc.Spec.RoutingTable, c.Spec.RoutingTable)
}

func NewEmptyConfig(name string) *NodeNetworkConfig {
return &NodeNetworkConfig{
ObjectMeta: metav1.ObjectMeta{Name: name},
Spec: NodeNetworkConfigSpec{
Vrf: []VRFRouteConfigurationSpec{},
Layer2: []Layer2NetworkConfigurationSpec{},
RoutingTable: []RoutingTableSpec{},
},
Status: NodeNetworkConfigStatus{
ConfigStatus: "",
},
}
}

func init() {
SchemeBuilder.Register(&NodeNetworkConfig{}, &NodeNetworkConfigList{})
}
Loading

0 comments on commit bb8c516

Please sign in to comment.