diff --git a/.dockerignore b/.dockerignore index 9f80101..2dbcd6e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -10,3 +10,6 @@ # Re-include Go module files !go.mod !go.sum + +# Re-include assets embedded via go:embed +!sonic/templates/** diff --git a/Dockerfile b/Dockerfile index 5530b7d..a58396c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,23 +17,20 @@ COPY . . # Build the controller-manager and Fabriclet independently so each final image # contains only the binary it needs. FROM builder AS fabric-controller-manager-builder -RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -o /out/fabric-controller-manager ./cmd/fabric-controller-manager +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o fabric-controller-manager ./cmd/fabric-controller-manager FROM builder AS fabriclet-sonic-builder -RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -o /out/fabriclet-sonic ./cmd/fabriclet-sonic +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o fabriclet-sonic ./cmd/fabriclet-sonic FROM gcr.io/distroless/static:nonroot AS fabric-controller-manager WORKDIR / -COPY --from=fabric-controller-manager-builder /out/fabric-controller-manager /fabric-controller-manager +COPY --from=fabric-controller-manager-builder /workspace/fabric-controller-manager /fabric-controller-manager USER 65532:65532 ENTRYPOINT ["/fabric-controller-manager"] -# Use distroless as minimal base image to package the manager binary -# Refer to https://github.com/GoogleContainerTools/distroless for more details -FROM gcr.io/distroless/static:nonroot AS fabriclet-sonic +FROM debian:bookworm-slim AS fabriclet-sonic WORKDIR / -COPY --from=fabriclet-sonic-builder /out/fabriclet-sonic /fabriclet-sonic -USER 65532:65532 +COPY --from=fabriclet-sonic-builder /workspace/fabriclet-sonic /fabriclet-sonic ENTRYPOINT ["/fabriclet-sonic"] diff --git a/api/v1alpha1/applyconfiguration/api/v1alpha1/cell.go b/api/v1alpha1/applyconfiguration/api/v1alpha1/cell.go index a0d2562..b172f14 100644 --- a/api/v1alpha1/applyconfiguration/api/v1alpha1/cell.go +++ b/api/v1alpha1/applyconfiguration/api/v1alpha1/cell.go @@ -6,11 +6,8 @@ package v1alpha1 import ( - apiv1alpha1 "github.com/ironcore-dev/fabric/api/v1alpha1" - internal "github.com/ironcore-dev/fabric/api/v1alpha1/applyconfiguration/internal" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" types "k8s.io/apimachinery/pkg/types" - managedfields "k8s.io/apimachinery/pkg/util/managedfields" v1 "k8s.io/client-go/applyconfigurations/meta/v1" ) @@ -19,7 +16,7 @@ import ( // // Cell is the Schema for the cells API type CellApplyConfiguration struct { - v1.TypeMetaApplyConfiguration `json:""` + v1.TypeMetaApplyConfiguration `json:",inline"` // metadata is a standard object metadata *v1.ObjectMetaApplyConfiguration `json:"metadata,omitempty"` // spec defines the desired state of Cell @@ -39,47 +36,6 @@ func Cell(name, namespace string) *CellApplyConfiguration { return b } -// ExtractCellFrom extracts the applied configuration owned by fieldManager from -// cell for the specified subresource. Pass an empty string for subresource to extract -// the main resource. Common subresources include "status", "scale", etc. -// cell must be a unmodified Cell API object that was retrieved from the Kubernetes API. -// ExtractCellFrom provides a way to perform a extract/modify-in-place/apply workflow. -// Note that an extracted apply configuration will contain fewer fields than what the fieldManager previously -// applied if another fieldManager has updated or force applied any of the previously applied fields. -func ExtractCellFrom(cell *apiv1alpha1.Cell, fieldManager string, subresource string) (*CellApplyConfiguration, error) { - b := &CellApplyConfiguration{} - err := managedfields.ExtractInto(cell, internal.Parser().Type("com.github.ironcore-dev.fabric.api.v1alpha1.Cell"), fieldManager, b, subresource) - if err != nil { - return nil, err - } - b.WithName(cell.Name) - b.WithNamespace(cell.Namespace) - - b.WithKind("Cell") - b.WithAPIVersion("fabric.ironcore.dev/v1alpha1") - return b, nil -} - -// ExtractCell extracts the applied configuration owned by fieldManager from -// cell. If no managedFields are found in cell for fieldManager, a -// CellApplyConfiguration is returned with only the Name, Namespace (if applicable), -// APIVersion and Kind populated. It is possible that no managed fields were found for because other -// field managers have taken ownership of all the fields previously owned by fieldManager, or because -// the fieldManager never owned fields any fields. -// cell must be a unmodified Cell API object that was retrieved from the Kubernetes API. -// ExtractCell provides a way to perform a extract/modify-in-place/apply workflow. -// Note that an extracted apply configuration will contain fewer fields than what the fieldManager previously -// applied if another fieldManager has updated or force applied any of the previously applied fields. -func ExtractCell(cell *apiv1alpha1.Cell, fieldManager string) (*CellApplyConfiguration, error) { - return ExtractCellFrom(cell, fieldManager, "") -} - -// ExtractCellStatus extracts the applied configuration owned by fieldManager from -// cell for the status subresource. -func ExtractCellStatus(cell *apiv1alpha1.Cell, fieldManager string) (*CellApplyConfiguration, error) { - return ExtractCellFrom(cell, fieldManager, "status") -} - func (b CellApplyConfiguration) IsApplyConfiguration() {} // WithKind sets the Kind field in the declarative configuration to the given value diff --git a/api/v1alpha1/applyconfiguration/api/v1alpha1/interface.go b/api/v1alpha1/applyconfiguration/api/v1alpha1/interface.go index 1f645cb..b0dbb35 100644 --- a/api/v1alpha1/applyconfiguration/api/v1alpha1/interface.go +++ b/api/v1alpha1/applyconfiguration/api/v1alpha1/interface.go @@ -6,11 +6,8 @@ package v1alpha1 import ( - apiv1alpha1 "github.com/ironcore-dev/fabric/api/v1alpha1" - internal "github.com/ironcore-dev/fabric/api/v1alpha1/applyconfiguration/internal" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" types "k8s.io/apimachinery/pkg/types" - managedfields "k8s.io/apimachinery/pkg/util/managedfields" v1 "k8s.io/client-go/applyconfigurations/meta/v1" ) @@ -19,7 +16,7 @@ import ( // // Interface is the Schema for the interfaces API type InterfaceApplyConfiguration struct { - v1.TypeMetaApplyConfiguration `json:""` + v1.TypeMetaApplyConfiguration `json:",inline"` // metadata is a standard object metadata *v1.ObjectMetaApplyConfiguration `json:"metadata,omitempty"` // spec defines the desired state of Interface @@ -30,54 +27,15 @@ type InterfaceApplyConfiguration struct { // Interface constructs a declarative configuration of the Interface type for use with // apply. -func Interface(name string) *InterfaceApplyConfiguration { +func Interface(name, namespace string) *InterfaceApplyConfiguration { b := &InterfaceApplyConfiguration{} b.WithName(name) + b.WithNamespace(namespace) b.WithKind("Interface") b.WithAPIVersion("fabric.ironcore.dev/v1alpha1") return b } -// ExtractInterfaceFrom extracts the applied configuration owned by fieldManager from -// interface for the specified subresource. Pass an empty string for subresource to extract -// the main resource. Common subresources include "status", "scale", etc. -// interface must be a unmodified Interface API object that was retrieved from the Kubernetes API. -// ExtractInterfaceFrom provides a way to perform a extract/modify-in-place/apply workflow. -// Note that an extracted apply configuration will contain fewer fields than what the fieldManager previously -// applied if another fieldManager has updated or force applied any of the previously applied fields. -func ExtractInterfaceFrom(iface *apiv1alpha1.Interface, fieldManager string, subresource string) (*InterfaceApplyConfiguration, error) { - b := &InterfaceApplyConfiguration{} - err := managedfields.ExtractInto(iface, internal.Parser().Type("com.github.ironcore-dev.fabric.api.v1alpha1.Interface"), fieldManager, b, subresource) - if err != nil { - return nil, err - } - b.WithName(iface.Name) - - b.WithKind("Interface") - b.WithAPIVersion("fabric.ironcore.dev/v1alpha1") - return b, nil -} - -// ExtractInterface extracts the applied configuration owned by fieldManager from -// interface. If no managedFields are found in interface for fieldManager, a -// InterfaceApplyConfiguration is returned with only the Name, Namespace (if applicable), -// APIVersion and Kind populated. It is possible that no managed fields were found for because other -// field managers have taken ownership of all the fields previously owned by fieldManager, or because -// the fieldManager never owned fields any fields. -// interface must be a unmodified Interface API object that was retrieved from the Kubernetes API. -// ExtractInterface provides a way to perform a extract/modify-in-place/apply workflow. -// Note that an extracted apply configuration will contain fewer fields than what the fieldManager previously -// applied if another fieldManager has updated or force applied any of the previously applied fields. -func ExtractInterface(iface *apiv1alpha1.Interface, fieldManager string) (*InterfaceApplyConfiguration, error) { - return ExtractInterfaceFrom(iface, fieldManager, "") -} - -// ExtractInterfaceStatus extracts the applied configuration owned by fieldManager from -// interface for the status subresource. -func ExtractInterfaceStatus(iface *apiv1alpha1.Interface, fieldManager string) (*InterfaceApplyConfiguration, error) { - return ExtractInterfaceFrom(iface, fieldManager, "status") -} - func (b InterfaceApplyConfiguration) IsApplyConfiguration() {} // WithKind sets the Kind field in the declarative configuration to the given value diff --git a/api/v1alpha1/applyconfiguration/api/v1alpha1/node.go b/api/v1alpha1/applyconfiguration/api/v1alpha1/node.go index ef1f1a8..211bae8 100644 --- a/api/v1alpha1/applyconfiguration/api/v1alpha1/node.go +++ b/api/v1alpha1/applyconfiguration/api/v1alpha1/node.go @@ -6,11 +6,8 @@ package v1alpha1 import ( - apiv1alpha1 "github.com/ironcore-dev/fabric/api/v1alpha1" - internal "github.com/ironcore-dev/fabric/api/v1alpha1/applyconfiguration/internal" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" types "k8s.io/apimachinery/pkg/types" - managedfields "k8s.io/apimachinery/pkg/util/managedfields" v1 "k8s.io/client-go/applyconfigurations/meta/v1" ) @@ -19,7 +16,7 @@ import ( // // Node is the Schema for the nodes API type NodeApplyConfiguration struct { - v1.TypeMetaApplyConfiguration `json:""` + v1.TypeMetaApplyConfiguration `json:",inline"` // metadata is a standard object metadata *v1.ObjectMetaApplyConfiguration `json:"metadata,omitempty"` // spec defines the desired state of Node @@ -30,54 +27,15 @@ type NodeApplyConfiguration struct { // Node constructs a declarative configuration of the Node type for use with // apply. -func Node(name string) *NodeApplyConfiguration { +func Node(name, namespace string) *NodeApplyConfiguration { b := &NodeApplyConfiguration{} b.WithName(name) + b.WithNamespace(namespace) b.WithKind("Node") b.WithAPIVersion("fabric.ironcore.dev/v1alpha1") return b } -// ExtractNodeFrom extracts the applied configuration owned by fieldManager from -// node for the specified subresource. Pass an empty string for subresource to extract -// the main resource. Common subresources include "status", "scale", etc. -// node must be a unmodified Node API object that was retrieved from the Kubernetes API. -// ExtractNodeFrom provides a way to perform a extract/modify-in-place/apply workflow. -// Note that an extracted apply configuration will contain fewer fields than what the fieldManager previously -// applied if another fieldManager has updated or force applied any of the previously applied fields. -func ExtractNodeFrom(node *apiv1alpha1.Node, fieldManager string, subresource string) (*NodeApplyConfiguration, error) { - b := &NodeApplyConfiguration{} - err := managedfields.ExtractInto(node, internal.Parser().Type("com.github.ironcore-dev.fabric.api.v1alpha1.Node"), fieldManager, b, subresource) - if err != nil { - return nil, err - } - b.WithName(node.Name) - - b.WithKind("Node") - b.WithAPIVersion("fabric.ironcore.dev/v1alpha1") - return b, nil -} - -// ExtractNode extracts the applied configuration owned by fieldManager from -// node. If no managedFields are found in node for fieldManager, a -// NodeApplyConfiguration is returned with only the Name, Namespace (if applicable), -// APIVersion and Kind populated. It is possible that no managed fields were found for because other -// field managers have taken ownership of all the fields previously owned by fieldManager, or because -// the fieldManager never owned fields any fields. -// node must be a unmodified Node API object that was retrieved from the Kubernetes API. -// ExtractNode provides a way to perform a extract/modify-in-place/apply workflow. -// Note that an extracted apply configuration will contain fewer fields than what the fieldManager previously -// applied if another fieldManager has updated or force applied any of the previously applied fields. -func ExtractNode(node *apiv1alpha1.Node, fieldManager string) (*NodeApplyConfiguration, error) { - return ExtractNodeFrom(node, fieldManager, "") -} - -// ExtractNodeStatus extracts the applied configuration owned by fieldManager from -// node for the status subresource. -func ExtractNodeStatus(node *apiv1alpha1.Node, fieldManager string) (*NodeApplyConfiguration, error) { - return ExtractNodeFrom(node, fieldManager, "status") -} - func (b NodeApplyConfiguration) IsApplyConfiguration() {} // WithKind sets the Kind field in the declarative configuration to the given value diff --git a/api/v1alpha1/applyconfiguration/internal/internal.go b/api/v1alpha1/applyconfiguration/internal/internal.go index 241e8ff..45bac10 100644 --- a/api/v1alpha1/applyconfiguration/internal/internal.go +++ b/api/v1alpha1/applyconfiguration/internal/internal.go @@ -26,334 +26,6 @@ func Parser() *typed.Parser { var parserOnce sync.Once var parser *typed.Parser var schemaYAML = typed.YAMLObject(`types: -- name: com.github.ironcore-dev.fabric.api.v1alpha1.AdminState - scalar: string -- name: com.github.ironcore-dev.fabric.api.v1alpha1.Cell - map: - fields: - - name: apiVersion - type: - scalar: string - - name: kind - type: - scalar: string - - name: metadata - type: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta - - name: spec - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.CellSpec - - name: status - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.CellStatus -- name: com.github.ironcore-dev.fabric.api.v1alpha1.CellPhase - scalar: string -- name: com.github.ironcore-dev.fabric.api.v1alpha1.CellSpec - map: - fields: - - name: hostname - type: - scalar: string - - name: id - type: - scalar: string - - name: ips - type: - list: - elementType: - scalar: string - elementRelationship: atomic - - name: nodeRef - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.LocalObjectReference - - name: peers - type: - list: - elementType: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.Peer - elementRelationship: atomic - - name: prefixes - type: - list: - elementType: - scalar: string - elementRelationship: atomic -- name: com.github.ironcore-dev.fabric.api.v1alpha1.CellStatus - map: - fields: - - name: conditions - type: - list: - elementType: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.Condition - elementRelationship: associative - keys: - - type - - name: phase - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.CellPhase -- name: com.github.ironcore-dev.fabric.api.v1alpha1.Interface - map: - fields: - - name: apiVersion - type: - scalar: string - - name: kind - type: - scalar: string - - name: metadata - type: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta - - name: spec - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.InterfaceSpec - - name: status - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.InterfaceStatus -- name: com.github.ironcore-dev.fabric.api.v1alpha1.InterfaceSpec - map: - fields: - - name: adminState - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.AdminState - default: Up - - name: handle - type: - scalar: string - - name: nodeRef - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.LocalObjectReference -- name: com.github.ironcore-dev.fabric.api.v1alpha1.InterfaceStatus - map: - fields: - - name: conditions - type: - list: - elementType: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.Condition - elementRelationship: associative - keys: - - type - - name: operationState - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.OperationState -- name: com.github.ironcore-dev.fabric.api.v1alpha1.LocalObjectReference - map: - fields: - - name: name - type: - scalar: string -- name: com.github.ironcore-dev.fabric.api.v1alpha1.NamespacedUIDReference - map: - fields: - - name: name - type: - scalar: string - - name: namespace - type: - scalar: string - - name: uid - type: - namedType: io.k8s.apimachinery.pkg.types.UID -- name: com.github.ironcore-dev.fabric.api.v1alpha1.Node - map: - fields: - - name: apiVersion - type: - scalar: string - - name: kind - type: - scalar: string - - name: metadata - type: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta - - name: spec - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.NodeSpec - - name: status - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.NodeStatus -- name: com.github.ironcore-dev.fabric.api.v1alpha1.NodeSpec - map: - fields: - - name: cellRef - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.NamespacedUIDReference - - name: providerID - type: - scalar: string -- name: com.github.ironcore-dev.fabric.api.v1alpha1.NodeStatus - map: - fields: - - name: conditions - type: - list: - elementType: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.Condition - elementRelationship: associative - keys: - - type -- name: com.github.ironcore-dev.fabric.api.v1alpha1.OperationState - scalar: string -- name: com.github.ironcore-dev.fabric.api.v1alpha1.Peer - map: - fields: - - name: dhcpRelay - type: - scalar: string - - name: interfaceRef - type: - namedType: com.github.ironcore-dev.fabric.api.v1alpha1.LocalObjectReference -- name: io.k8s.apimachinery.pkg.apis.meta.v1.Condition - map: - fields: - - name: lastTransitionTime - type: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.Time - - name: message - type: - scalar: string - - name: observedGeneration - type: - scalar: numeric - - name: reason - type: - scalar: string - - name: status - type: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.ConditionStatus - - name: type - type: - scalar: string -- name: io.k8s.apimachinery.pkg.apis.meta.v1.ConditionStatus - scalar: string -- name: io.k8s.apimachinery.pkg.apis.meta.v1.FieldsV1 - map: - elementType: - scalar: untyped - list: - elementType: - namedType: __untyped_atomic_ - elementRelationship: atomic - map: - elementType: - namedType: __untyped_deduced_ - elementRelationship: separable -- name: io.k8s.apimachinery.pkg.apis.meta.v1.ManagedFieldsEntry - map: - fields: - - name: apiVersion - type: - scalar: string - - name: fieldsType - type: - scalar: string - - name: fieldsV1 - type: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.FieldsV1 - - name: manager - type: - scalar: string - - name: operation - type: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.ManagedFieldsOperationType - - name: subresource - type: - scalar: string - - name: time - type: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.Time -- name: io.k8s.apimachinery.pkg.apis.meta.v1.ManagedFieldsOperationType - scalar: string -- name: io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta - map: - fields: - - name: annotations - type: - map: - elementType: - scalar: string - - name: creationTimestamp - type: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.Time - - name: deletionGracePeriodSeconds - type: - scalar: numeric - - name: deletionTimestamp - type: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.Time - - name: finalizers - type: - list: - elementType: - scalar: string - elementRelationship: associative - - name: generateName - type: - scalar: string - - name: generation - type: - scalar: numeric - - name: labels - type: - map: - elementType: - scalar: string - - name: managedFields - type: - list: - elementType: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.ManagedFieldsEntry - elementRelationship: atomic - - name: name - type: - scalar: string - - name: namespace - type: - scalar: string - - name: ownerReferences - type: - list: - elementType: - namedType: io.k8s.apimachinery.pkg.apis.meta.v1.OwnerReference - elementRelationship: associative - keys: - - uid - - name: resourceVersion - type: - scalar: string - - name: selfLink - type: - scalar: string - - name: uid - type: - namedType: io.k8s.apimachinery.pkg.types.UID -- name: io.k8s.apimachinery.pkg.apis.meta.v1.OwnerReference - map: - fields: - - name: apiVersion - type: - scalar: string - - name: blockOwnerDeletion - type: - scalar: boolean - - name: controller - type: - scalar: boolean - - name: kind - type: - scalar: string - - name: name - type: - scalar: string - - name: uid - type: - namedType: io.k8s.apimachinery.pkg.types.UID - elementRelationship: atomic -- name: io.k8s.apimachinery.pkg.apis.meta.v1.Time - scalar: untyped -- name: io.k8s.apimachinery.pkg.types.UID - scalar: string - name: __untyped_atomic_ scalar: untyped list: diff --git a/cellruntime/cellruntime.go b/cellruntime/cellruntime.go index 6062f54..5a3793b 100644 --- a/cellruntime/cellruntime.go +++ b/cellruntime/cellruntime.go @@ -13,6 +13,7 @@ type CellMetadata struct { Namespace string Name string UID string + Labels map[string]string } type CellConfig struct { diff --git a/cmd/fabriclet-sonic/main.go b/cmd/fabriclet-sonic/main.go index 7c8bb6a..690542b 100644 --- a/cmd/fabriclet-sonic/main.go +++ b/cmd/fabriclet-sonic/main.go @@ -4,9 +4,11 @@ package main import ( + "fmt" "os" "github.com/ironcore-dev/fabric/api/v1alpha1" + "github.com/ironcore-dev/fabric/cellruntime" "github.com/ironcore-dev/fabric/fabriclet/cli" "github.com/ironcore-dev/fabric/sonic" "github.com/spf13/pflag" @@ -17,11 +19,12 @@ import ( func main() { var flags cli.Flags - var name string + var name, backend string var interfaces []string flags.AddFlags(pflag.CommandLine) pflag.StringVar(&name, "name", name, "Name of the node") + pflag.StringVar(&backend, "backend", "script", "Runtime implementation to use (script, configdb)") pflag.StringSliceVarP(&interfaces, "interface", "I", interfaces, "Names of the interfaces to reconcile") pflag.Parse() @@ -36,7 +39,16 @@ func main() { interfaceSet := sets.New(interfaces...) - prov, err := sonic.NewRuntime() + var prov cellruntime.Runtime + var err error + switch backend { + case "script": + prov = sonic.NewScriptRuntime() + case "configdb": + prov = sonic.NewConfigDBRuntime() + default: + err = fmt.Errorf("unknown backend %q", backend) + } if err != nil { setupLog.Error(err, "Error creating sonic runtime") os.Exit(1) diff --git a/config/samples/fabric_v1alpha1_cell.yaml b/config/samples/fabric_v1alpha1_cell.yaml index e53e05d..d5e80d9 100644 --- a/config/samples/fabric_v1alpha1_cell.yaml +++ b/config/samples/fabric_v1alpha1_cell.yaml @@ -1,3 +1,5 @@ +# SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors +# SPDX-License-Identifier: Apache-2.0 apiVersion: fabric.ironcore.dev/v1alpha1 kind: Cell metadata: @@ -6,4 +8,18 @@ metadata: app.kubernetes.io/managed-by: kustomize name: cell-sample spec: - # TODO(user): Add fields here + nodeRef: + name: swi1-ab-42 + id: "42" + hostname: swi1-ab-42.fabric.example.com + ips: + - fd00:1234:5678:f2:: + # prefixes[0] is the advertised summary, prefixes[1+i] the interface + # prefix of peers[i]. + prefixes: + - fd00:1234:5678:f2::/64 + - fd00:1234:5678:f2:1::/80 + peers: + - interfaceRef: + name: Ethernet0 + dhcpRelay: fd00:1234:5678:3201::1:547 diff --git a/fabriclet/controller/init.go b/fabriclet/controller/init.go index 99c9c52..56d9521 100644 --- a/fabriclet/controller/init.go +++ b/fabriclet/controller/init.go @@ -23,7 +23,7 @@ type InitOptions struct { func Init(ctx context.Context, c client.Client, nodeName, providerID string, opts InitOptions) error { fieldOwner := client.FieldOwner("node.fabriclet.ironcore.dev/" + nodeName) - node := v1alpha1.Node(nodeName). + node := v1alpha1.Node(nodeName, ""). WithSpec(v1alpha1.NodeSpec(). WithProviderID(providerID)) if err := c.Apply(ctx, node, fieldOwner); err != nil { @@ -31,7 +31,7 @@ func Init(ctx context.Context, c client.Client, nodeName, providerID string, opt } for _, ifaceOpts := range opts.Interfaces { - iface := v1alpha1.Interface(ifaceOpts.Name). + iface := v1alpha1.Interface(ifaceOpts.Name, ""). WithSpec(v1alpha1.InterfaceSpec(). WithNodeRef(v1alpha1.LocalObjectReference().WithName(nodeName)). WithHandle(ifaceOpts.Handle)) diff --git a/fabriclet/internal/controller/cell_controller.go b/fabriclet/internal/controller/cell_controller.go index 3dd278b..46e5997 100644 --- a/fabriclet/internal/controller/cell_controller.go +++ b/fabriclet/internal/controller/cell_controller.go @@ -344,6 +344,7 @@ func (r *CellReconciler) resolveCellConfig( Namespace: cell.Namespace, Name: cell.Name, UID: string(cell.UID), + Labels: cell.Labels, }, ID: cell.Spec.ID, Hostname: cell.Spec.Hostname, diff --git a/go.mod b/go.mod index ea7281b..0461b02 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/ironcore-dev/fabric go 1.25.7 require ( + github.com/google/go-cmp v0.7.0 github.com/onsi/ginkgo/v2 v2.33.1 github.com/onsi/gomega v1.44.0 github.com/spf13/pflag v1.0.10 @@ -38,7 +39,6 @@ require ( github.com/google/btree v1.1.3 // indirect github.com/google/cel-go v0.26.0 // indirect github.com/google/gnostic-models v0.7.0 // indirect - github.com/google/go-cmp v0.7.0 // indirect github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect diff --git a/sonic/configdb.go b/sonic/configdb.go new file mode 100644 index 0000000..5cfeab5 --- /dev/null +++ b/sonic/configdb.go @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-License-Identifier: Apache-2.0 + +package sonic + +import ( + "context" + "errors" + + "github.com/ironcore-dev/fabric/cellruntime" +) + +var errNotImplemented = errors.New("sonic configdb runtime: not implemented") + +// ConfigDBRuntime is a cellruntime.Runtime that writes directly to the SONiC CONFIG_DB. +// TODO: implement. The script-based ScriptRuntime is the first step; select the +// implementation with the fabriclet's --backend flag. +type ConfigDBRuntime struct{} + +var _ cellruntime.Runtime = (*ConfigDBRuntime)(nil) + +func NewConfigDBRuntime() *ConfigDBRuntime { + return &ConfigDBRuntime{} +} + +func (r *ConfigDBRuntime) ProviderName() string { + return ProviderName +} + +func (r *ConfigDBRuntime) NodeID(ctx context.Context, node string) (string, error) { + return "", errNotImplemented +} + +func (r *ConfigDBRuntime) ApplyCell(ctx context.Context, node string, cfg *cellruntime.CellConfig) error { + return errNotImplemented +} + +func (r *ConfigDBRuntime) DeleteCell(ctx context.Context, node string) error { + return errNotImplemented +} + +func (r *ConfigDBRuntime) CellStatus(ctx context.Context, node string) (*cellruntime.CellStatus, error) { + return nil, errNotImplemented +} + +func (r *ConfigDBRuntime) InterfaceID(ctx context.Context, iface string) (string, error) { + return "", errNotImplemented +} + +func (r *ConfigDBRuntime) InterfaceState(ctx context.Context, iface string) (*cellruntime.InterfaceState, error) { + return nil, errNotImplemented +} + +func (r *ConfigDBRuntime) SetInterfaceAdminState(ctx context.Context, iface string, adminState bool) error { + return errNotImplemented +} diff --git a/sonic/runtime.go b/sonic/runtime.go deleted file mode 100644 index f648e77..0000000 --- a/sonic/runtime.go +++ /dev/null @@ -1,57 +0,0 @@ -// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors -// SPDX-License-Identifier: Apache-2.0 - -package sonic - -import ( - "context" - - "github.com/ironcore-dev/fabric/cellruntime" -) - -type Runtime struct { -} - -func NewRuntime() (*Runtime, error) { - panic("implement me") -} - -func (r *Runtime) ProviderName() string { - // TODO implement me - panic("implement me") -} - -func (r *Runtime) NodeID(ctx context.Context, node string) (string, error) { - // TODO implement me - panic("implement me") -} - -func (r *Runtime) ApplyCell(ctx context.Context, node string, cfg *cellruntime.CellConfig) error { - // TODO implement me - panic("implement me") -} - -func (r *Runtime) DeleteCell(ctx context.Context, node string) error { - // TODO implement me - panic("implement me") -} - -func (r *Runtime) CellStatus(ctx context.Context, node string) (*cellruntime.CellStatus, error) { - // TODO implement me - panic("implement me") -} - -func (r *Runtime) InterfaceID(ctx context.Context, iface string) (string, error) { - // TODO implement me - panic("implement me") -} - -func (r *Runtime) InterfaceState(ctx context.Context, iface string) (*cellruntime.InterfaceState, error) { - // TODO implement me - panic("implement me") -} - -func (r *Runtime) SetInterfaceAdminState(ctx context.Context, iface string, adminState bool) error { - // TODO implement me - panic("implement me") -} diff --git a/sonic/script.go b/sonic/script.go new file mode 100644 index 0000000..24a6162 --- /dev/null +++ b/sonic/script.go @@ -0,0 +1,257 @@ +// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-License-Identifier: Apache-2.0 + +package sonic + +import ( + "bytes" + "context" + "embed" + "fmt" + "net/netip" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "text/template" + + "github.com/ironcore-dev/fabric/cellruntime" + "sigs.k8s.io/controller-runtime/pkg/log" +) + +const ( + ProviderName = "sonic" + doneFlagFile = "/etc/sonic/fabric-cell.done" + scriptDir = "/etc/sonic" +) + +// CellRoleLabel labels a cell with the role the switch gets provisioned +// with (fabric.ironcore.dev/role). +const CellRoleLabel = "fabric.ironcore.dev/role" + +// Cell roles a cell can be labeled with. +const ( + RoleLeaf = "leaf" + RoleSpine = "spine" +) + +//go:embed templates +var templates embed.FS + +type ScriptRuntime struct{} + +var _ cellruntime.Runtime = (*ScriptRuntime)(nil) + +func NewScriptRuntime() *ScriptRuntime { + return &ScriptRuntime{} +} + +func (r *ScriptRuntime) ProviderName() string { + return ProviderName +} + +func (r *ScriptRuntime) NodeID(ctx context.Context, node string) (string, error) { + return node, nil +} + +func (r *ScriptRuntime) ApplyCell(ctx context.Context, node string, cfg *cellruntime.CellConfig) error { + script, err := r.renderCell(cfg) + if err != nil { + return err + } + return runScript(ctx, script) +} + +func (r *ScriptRuntime) DeleteCell(ctx context.Context, node string) error { + if !fileExists(doneFlagFile) { + return fmt.Errorf("cell %s %w", node, cellruntime.ErrNotFound) + } + script, err := r.renderReset(node) + if err != nil { + return err + } + return runScript(ctx, script) +} + +type southPort struct { + Port string + VLAN int + IP string + Relay string +} + +type switchTemplateData struct { + Hostname string + ASN int64 + RouterID string + LoopbackIPs []string + Summary string + AllPorts []string + SouthPorts []southPort + FabricPorts []string + IsLeaf bool +} + +func (r *ScriptRuntime) renderCell(cfg *cellruntime.CellConfig) ([]byte, error) { + id, err := strconv.Atoi(cfg.ID) + if err != nil { + return nil, cellruntime.TerminalError(fmt.Errorf("cell ID %q must be numeric: %w", cfg.ID, err)) + } + cell := cfg.Metadata.Namespace + "/" + cfg.Metadata.Name + switch { + case cfg.Hostname == "": + return nil, terminalCellErrorf(cell, "must set a hostname") + case len(cfg.LoopbackIPs) == 0: + return nil, terminalCellErrorf(cell, "must set at least one loopback IP") + case len(cfg.Prefixes) == 0: + return nil, terminalCellErrorf(cell, "must set at least one prefix") + case len(cfg.Peers) == 0: + return nil, terminalCellErrorf(cell, "must set at least one peer") + } + + // The cell's role label picks ASN base and router ID base. + role := cfg.Metadata.Labels[CellRoleLabel] + var asnBase int64 + var routerIDBase string + var isLeaf bool + switch role { + case RoleLeaf: + asnBase, routerIDBase, isLeaf = 4211000000, "1.0", true + case RoleSpine: + asnBase, routerIDBase = 4212000000, "2.0" + default: + return nil, terminalCellErrorf(cell, "must be labeled %s %s or %s, got %q", + CellRoleLabel, RoleLeaf, RoleSpine, role) + } + + // Peers with a DHCP relay are south-facing server ports, peers without + // one are fabric-facing routed ports. + var southPeers, fabricPeers []cellruntime.Peer + for _, peer := range cfg.Peers { + if peer.Interface == nil { + return nil, terminalCellErrorf(cell, "has a peer without interface") + } + if peer.DHCPRelay == "" { + fabricPeers = append(fabricPeers, peer) + continue + } + if _, err := netip.ParseAddr(peer.DHCPRelay); err != nil { + return nil, terminalCellErrorf(cell, "has invalid DHCP relay %q", peer.DHCPRelay) + } + southPeers = append(southPeers, peer) + } + if role == RoleSpine && len(southPeers) > 0 { + return nil, terminalCellErrorf(cell, "has role %s but peers with a DHCP relay", RoleSpine) + } + + // The first prefix is the summary that gets advertised and rejected, + // prefixes[1+i] is the interface prefix of the i-th south peer. + if len(cfg.Prefixes) != len(southPeers)+1 { + return nil, terminalCellErrorf(cell, "must set %d prefixes, got %d", len(southPeers)+1, len(cfg.Prefixes)) + } + + data := &switchTemplateData{ + Hostname: cfg.Hostname, + ASN: asnBase + int64(id), + RouterID: fmt.Sprintf("%s.%d.%d", routerIDBase, id/256, id%256), + Summary: cfg.Prefixes[0].String(), + IsLeaf: isLeaf, + } + for _, ip := range cfg.LoopbackIPs { + data.LoopbackIPs = append(data.LoopbackIPs, ip.String()) + } + for i, peer := range southPeers { + portNum, err := strconv.Atoi(strings.TrimPrefix(peer.Interface.ID, "Ethernet")) + if err != nil { + return nil, terminalCellErrorf(cell, "has invalid Ethernet port ID %q", peer.Interface.ID) + } + data.SouthPorts = append(data.SouthPorts, southPort{ + Port: peer.Interface.ID, + VLAN: portNum/4 + 1001, + IP: cfg.Prefixes[i+1].String(), + Relay: peer.DHCPRelay, + }) + data.AllPorts = append(data.AllPorts, peer.Interface.ID) + } + for _, peer := range fabricPeers { + data.FabricPorts = append(data.FabricPorts, peer.Interface.ID) + data.AllPorts = append(data.AllPorts, peer.Interface.ID) + } + + return render("switch.sh", data) +} + +func (r *ScriptRuntime) renderReset(node string) ([]byte, error) { + return render("reset.sh", struct{ Node string }{Node: node}) +} + +func (r *ScriptRuntime) CellStatus(ctx context.Context, node string) (*cellruntime.CellStatus, error) { + if !fileExists(doneFlagFile) { + return nil, fmt.Errorf("cell %s %w", node, cellruntime.ErrNotFound) + } + return &cellruntime.CellStatus{Phase: cellruntime.CellPhaseActive}, nil +} + +func (r *ScriptRuntime) InterfaceID(ctx context.Context, iface string) (string, error) { + return iface, nil +} + +func (r *ScriptRuntime) InterfaceState(ctx context.Context, iface string) (*cellruntime.InterfaceState, error) { + operstate, err := os.ReadFile(filepath.Join("/sys/class/net", iface, "operstate")) + if err != nil { + return nil, fmt.Errorf("reading operstate of interface %s: %w", iface, err) + } + return &cellruntime.InterfaceState{ + Up: strings.TrimSpace(string(operstate)) == "up", + }, nil +} + +func (r *ScriptRuntime) SetInterfaceAdminState(ctx context.Context, iface string, adminState bool) error { + return nil +} + +func terminalCellErrorf(cell, format string, args ...any) error { + return cellruntime.TerminalError(fmt.Errorf("cell "+cell+": "+format, args...)) +} + +func fileExists(name string) bool { + _, err := os.Stat(name) + return err == nil +} + +func render(name string, data any) ([]byte, error) { + tmpl, err := template.ParseFS(templates, "templates/"+name) + if err != nil { + return nil, cellruntime.TerminalError(fmt.Errorf("parsing cell template: %w", err)) + } + var buf bytes.Buffer + if err := tmpl.Execute(&buf, data); err != nil { + return nil, cellruntime.TerminalError(fmt.Errorf("rendering cell template: %w", err)) + } + return buf.Bytes(), nil +} + +func runScript(ctx context.Context, script []byte) error { + f, err := os.CreateTemp(scriptDir, "fabric-cell-*.sh") + if err != nil { + return fmt.Errorf("creating cell script file: %w", err) + } + defer func() { _ = os.Remove(f.Name()) }() + + if _, err := f.Write(script); err != nil { + _ = f.Close() + return fmt.Errorf("writing cell script %s: %w", f.Name(), err) + } + if err := f.Close(); err != nil { + return fmt.Errorf("closing cell script %s: %w", f.Name(), err) + } + + out, err := exec.CommandContext(ctx, "bash", f.Name()).CombinedOutput() + if err != nil { + return cellruntime.TerminalError(fmt.Errorf("running cell script: %w: %s", err, strings.TrimSpace(string(out)))) + } + + log.FromContext(ctx).V(1).Info("Cell script completed", "output", strings.TrimSpace(string(out))) + return nil +} diff --git a/sonic/script_test.go b/sonic/script_test.go new file mode 100644 index 0000000..3ade5ec --- /dev/null +++ b/sonic/script_test.go @@ -0,0 +1,165 @@ +// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-License-Identifier: Apache-2.0 + +package sonic + +import ( + "errors" + "flag" + "fmt" + "net/netip" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/ironcore-dev/fabric/cellruntime" +) + +var update = flag.Bool("update", false, "update golden files") + +// compareGolden compares got with the golden file testdata/. +// Regenerate with: go test ./sonic/ -update +func compareGolden(t *testing.T, name string, got []byte) { + t.Helper() + + path := filepath.Join("testdata", name) + if *update { + if err := os.MkdirAll("testdata", 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, got, 0o644); err != nil { + t.Fatal(err) + } + t.Logf("updated %s", path) + return + } + + want, err := os.ReadFile(path) + if err != nil { + t.Fatalf("reading golden file %s: %v (run with -update to create it)", path, err) + } + if diff := cmp.Diff(string(want), string(got)); diff != "" { + t.Errorf("rendered script differs from %s (-want +got):\n%s", path, diff) + } +} + +// testCell builds a small leaf cell config: one summary prefix, one /80 +// interface prefix per south-facing server port (Ethernet0, 4) and two +// spine uplinks (Ethernet108, 112) as fabric peers. +func testCell(hostname string) *cellruntime.CellConfig { + prefixes := []netip.Prefix{netip.MustParsePrefix("fd00:1234:5678:f2::/64")} + peers := make([]cellruntime.Peer, 0, 4) + for i := 0; i <= 4; i += 4 { + prefixes = append(prefixes, netip.MustParsePrefix(fmt.Sprintf("fd00:1234:5678:f2:%04x::/80", i+1))) + peers = append(peers, cellruntime.Peer{ + Interface: &cellruntime.Interface{ + Metadata: cellruntime.InterfaceMetadata{Name: fmt.Sprintf("eth%d", i)}, + ID: fmt.Sprintf("Ethernet%d", i), + }, + DHCPRelay: "fd00:1234:5678:3201::1:547", + }) + } + for i := 108; i <= 112; i += 4 { + peers = append(peers, cellruntime.Peer{ + Interface: &cellruntime.Interface{ + Metadata: cellruntime.InterfaceMetadata{Name: fmt.Sprintf("eth%d", i)}, + ID: fmt.Sprintf("Ethernet%d", i), + }, + }) + } + return &cellruntime.CellConfig{ + Metadata: cellruntime.CellMetadata{ + Namespace: "default", Name: "cell-42", Labels: map[string]string{CellRoleLabel: RoleLeaf}, + }, + ID: "42", + Hostname: hostname, + LoopbackIPs: []netip.Addr{netip.MustParseAddr("fd00:1234:5678:f2::")}, + Prefixes: prefixes, + Peers: peers, + } +} + +func TestRenderCell(t *testing.T) { + for _, test := range []struct { + name string + hostname string + mutate func(*cellruntime.CellConfig) + }{ + {name: "leaf", hostname: "swi1-ab-42.fabric.example.com"}, + {name: "spine", hostname: "swi2-ab-7.fabric.example.com", mutate: func(c *cellruntime.CellConfig) { + // A spine cell has the spine role label, the summary prefix and + // only fabric peers. + c.Metadata.Labels[CellRoleLabel] = RoleSpine + var fabricPeers []cellruntime.Peer + for _, peer := range c.Peers { + if peer.DHCPRelay == "" { + fabricPeers = append(fabricPeers, peer) + } + } + c.Peers = fabricPeers + c.Prefixes = c.Prefixes[:1] + }}, + } { + t.Run(test.name, func(t *testing.T) { + cfg := testCell(test.hostname) + if test.mutate != nil { + test.mutate(cfg) + } + + got, err := NewScriptRuntime().renderCell(cfg) + if err != nil { + t.Fatalf("rendering cell: %v", err) + } + + if strings.Contains(string(got), "{{") { + t.Error("rendered script still contains a template action") + } + compareGolden(t, test.name+".golden.sh", got) + }) + } +} + +func TestRenderReset(t *testing.T) { + got, err := NewScriptRuntime().renderReset("swi1-ab-42") + if err != nil { + t.Fatalf("rendering reset: %v", err) + } + if strings.Contains(string(got), "{{") { + t.Error("rendered script still contains a template action") + } + compareGolden(t, "reset.golden.sh", got) +} + +func TestRenderCellInvalid(t *testing.T) { + rt := NewScriptRuntime() + + for name, mutate := range map[string]func(*cellruntime.CellConfig){ + "non-numeric ID": func(c *cellruntime.CellConfig) { c.ID = "abc" }, + "missing hostname": func(c *cellruntime.CellConfig) { c.Hostname = "" }, + "missing loopback IPs": func(c *cellruntime.CellConfig) { c.LoopbackIPs = nil }, + "missing prefixes": func(c *cellruntime.CellConfig) { c.Prefixes = nil }, + "no peers": func(c *cellruntime.CellConfig) { c.Peers = nil }, + "missing role": func(c *cellruntime.CellConfig) { delete(c.Metadata.Labels, CellRoleLabel) }, + "unknown role": func(c *cellruntime.CellConfig) { c.Metadata.Labels[CellRoleLabel] = "tor" }, + "spine with server peers": func(c *cellruntime.CellConfig) { + c.Metadata.Labels[CellRoleLabel] = RoleSpine + }, + "peer without interface": func(c *cellruntime.CellConfig) { + c.Peers[0].Interface = nil + }, + "peer with invalid DHCP relay": func(c *cellruntime.CellConfig) { + c.Peers[0].DHCPRelay = "not-an-ip" + }, + "prefix count mismatch": func(c *cellruntime.CellConfig) { c.Prefixes = c.Prefixes[:2] }, + } { + t.Run(name, func(t *testing.T) { + cfg := testCell("swi1-ab-42.fabric.example.com") + mutate(cfg) + if _, err := rt.renderCell(cfg); !errors.Is(err, cellruntime.TerminalError(nil)) { + t.Errorf("expected terminal error, got %v", err) + } + }) + } +} diff --git a/sonic/templates/reset.sh b/sonic/templates/reset.sh new file mode 100644 index 0000000..1f41a67 --- /dev/null +++ b/sonic/templates/reset.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors +# SPDX-License-Identifier: Apache-2.0 +set -e + +# When run inside a container, re-exec in the host namespaces so the +# config/systemctl/reboot calls below act on the host. +if [ -z "$FABRIC_ON_HOST" ]; then + export FABRIC_ON_HOST=1 + exec nsenter --target 1 --mount --net --pid bash "$0" +fi + +# Reset script for node {{ .Node }}, rendered by the fabric cell runtime. +# Undoes what the cell script applied: server VLANs, loopback IPs, port +# state and the FRR/BGP config. Everything is read from the running +# CONFIG_DB, so this also works on a partially provisioned device (failed +# cells). Deliberately kept: hostname, management config, link-local and +# the physical port parameters (mtu/fec/speed). + +CONFIG_DB=/etc/sonic/config_db.json + +# Remove the loopback addresses handed down by the cell +for ENTRY in $(jq -r '.LOOPBACK_INTERFACE // {} | keys[] | select(contains("|"))' "$CONFIG_DB"); do + config interface ip remove "${ENTRY%%|*}" "${ENTRY#*|}" +done + +# Remove the VLANs the cell's server peers created, in dependency order: +# DHCP relay, interface IPs, members, then the VLAN itself +for VLAN in $(jq -r '.VLAN // {} | keys[]' "$CONFIG_DB"); do + VLAN_ID="${VLAN#Vlan}" + for SERVER in $(jq -r --arg v "$VLAN" '.DHCP_RELAY[$v] // {} | .dhcpv6_servers // .dhcp_servers // [] | .[]' "$CONFIG_DB"); do + config vlan dhcp_relay del "$VLAN_ID" "$SERVER" + done + for ENTRY in $(jq -r --arg v "$VLAN" '.VLAN_INTERFACE // {} | keys[] | select(startswith($v + "|"))' "$CONFIG_DB"); do + config interface ip remove "$VLAN" "${ENTRY#*|}" + done + for MEMBER in $(jq -r --arg v "$VLAN" '.VLAN_MEMBER // {} | keys[] | select(startswith($v + "|"))' "$CONFIG_DB"); do + config vlan member del "$VLAN_ID" "${MEMBER#*|}" + done + config vlan del "$VLAN_ID" +done + +# Shut down every front panel port the cell script started up +for PORT in $(jq -r '.PORT // {} | to_entries[] | select(.value.admin_status == "up") | .key' "$CONFIG_DB"); do + config interface shutdown "$PORT" +done + +config save -y + +# Return FRR to an empty config: no BGP peering +cat </etc/sonic/frr/frr.conf +frr version 8.1 +frr defaults traditional +hostname $(hostname) +log syslog informational +service integrated-vtysh-config +EOF + +systemctl restart bgp + +# Mark the device as unprovisioned, let the fabriclet expire the cell +rm -f /etc/sonic/fabric-cell.done + +reboot diff --git a/sonic/templates/switch.sh b/sonic/templates/switch.sh new file mode 100644 index 0000000..652b8d9 --- /dev/null +++ b/sonic/templates/switch.sh @@ -0,0 +1,130 @@ +#!/bin/bash +# SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors +# SPDX-License-Identifier: Apache-2.0 +set -e + +# When run inside a container, re-exec in the host namespaces so the +# config/systemctl/reboot calls below act on the host. +if [ -z "$FABRIC_ON_HOST" ]; then + export FABRIC_ON_HOST=1 + exec nsenter --target 1 --mount --net --pid bash "$0" +fi + +# Flag file to track completion +FLAG_FILE_DONE="/etc/sonic/fabric-cell.done" + +if [ ! -f "$FLAG_FILE_DONE" ]; then + # Configure the loopback addresses handed down by the cell +{{- range .LoopbackIPs }} + config interface ip add Loopback0 {{ . }}/128 +{{- end }} + + # Configure the physical settings of all peer ports named by the cell +{{- range .AllPorts }} + config interface mtu {{ . }} 9100 + config interface fec {{ . }} rs + config interface speed {{ . }} 100000 + config interface startup {{ . }} +{{- end }} + + # Configure the south-facing server ports: one VLAN per port with its + # interface prefix and DHCP relay +{{- range .SouthPorts }} + config vlan add {{ .VLAN }} + config vlan member add {{ .VLAN }} {{ .Port }} -u + config interface ip add Vlan{{ .VLAN }} {{ .IP }} + config vlan dhcp_relay add {{ .VLAN }} {{ .Relay }} +{{- end }} + + # Enable IPv6 link-local + config ipv6 enable link-local + + # Save running config + config save -y + + # Update FRR (BGP) configuration + cat </etc/sonic/frr/frr.conf +frr version 8.1 +frr defaults traditional +hostname {{ .Hostname }} +log syslog informational +service integrated-vtysh-config +{{- range .SouthPorts }} + +interface Vlan{{ .VLAN }} + no ipv6 nd suppress-ra + ipv6 nd managed-config-flag + ipv6 nd other-config-flag +{{- end }} +router bgp {{ .ASN }} + bgp router-id {{ .RouterID }} + no bgp ebgp-requires-policy + no bgp default ipv4-unicast + bgp bestpath as-path multipath-relax + no bgp network import-check + + neighbor FABRIC peer-group + neighbor FABRIC remote-as external + neighbor FABRIC timers 3 9 + neighbor FABRIC timers connect 20 +{{- range .FabricPorts }} + neighbor {{ . }} interface peer-group FABRIC +{{- end }} +{{- if .SouthPorts }} + + neighbor SOUTH peer-group + neighbor SOUTH remote-as external + neighbor SOUTH timers 3 9 + neighbor SOUTH timers connect 20 +{{- range .SouthPorts }} + neighbor Vlan{{ .VLAN }} interface peer-group SOUTH +{{- end }} +{{- end }} + + address-family ipv6 unicast + network {{ .Summary }} + + neighbor FABRIC activate + neighbor FABRIC route-map RM_FABRIC_IN in + neighbor FABRIC route-map RM_FABRIC_OUT out +{{- if .SouthPorts }} + + neighbor SOUTH activate + neighbor SOUTH route-map RM_SOUTH_IN in + neighbor SOUTH route-map RM_SOUTH_OUT out +{{ else }} + +{{ end }} exit-address-family +exit + +{{ if .IsLeaf -}} +route-map RM_FABRIC_IN permit 10 + set community 65000:100 +! +bgp community-list 10 permit 65000:100 + +route-map RM_FABRIC_OUT deny 10 + match community 10 +route-map RM_FABRIC_OUT permit 20 +! +{{ else -}} +route-map RM_FABRIC_IN permit 10 +route-map RM_FABRIC_OUT permit 10 +! +{{ end -}} +{{ if .SouthPorts -}} +route-map RM_SOUTH_IN permit 10 +route-map RM_SOUTH_OUT permit 10 +! +{{ end -}} +ipv6 route {{ .Summary }} reject +EOF + + # Restart BGP to load new config + systemctl restart bgp + + # Mark the cell as applied + touch "$FLAG_FILE_DONE" + reboot +fi +exit 0 diff --git a/sonic/testdata/leaf.golden.sh b/sonic/testdata/leaf.golden.sh new file mode 100644 index 0000000..9e73fb0 --- /dev/null +++ b/sonic/testdata/leaf.golden.sh @@ -0,0 +1,128 @@ +#!/bin/bash +# SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors +# SPDX-License-Identifier: Apache-2.0 +set -e + +# When run inside a container, re-exec in the host namespaces so the +# config/systemctl/reboot calls below act on the host. +if [ -z "$FABRIC_ON_HOST" ]; then + export FABRIC_ON_HOST=1 + exec nsenter --target 1 --mount --net --pid bash "$0" +fi + +# Flag file to track completion +FLAG_FILE_DONE="/etc/sonic/fabric-cell.done" + +if [ ! -f "$FLAG_FILE_DONE" ]; then + # Configure the loopback addresses handed down by the cell + config interface ip add Loopback0 fd00:1234:5678:f2::/128 + + # Configure the physical settings of all peer ports named by the cell + config interface mtu Ethernet0 9100 + config interface fec Ethernet0 rs + config interface speed Ethernet0 100000 + config interface startup Ethernet0 + config interface mtu Ethernet4 9100 + config interface fec Ethernet4 rs + config interface speed Ethernet4 100000 + config interface startup Ethernet4 + config interface mtu Ethernet108 9100 + config interface fec Ethernet108 rs + config interface speed Ethernet108 100000 + config interface startup Ethernet108 + config interface mtu Ethernet112 9100 + config interface fec Ethernet112 rs + config interface speed Ethernet112 100000 + config interface startup Ethernet112 + + # Configure the south-facing server ports: one VLAN per port with its + # interface prefix and DHCP relay + config vlan add 1001 + config vlan member add 1001 Ethernet0 -u + config interface ip add Vlan1001 fd00:1234:5678:f2:1::/80 + config vlan dhcp_relay add 1001 fd00:1234:5678:3201::1:547 + config vlan add 1002 + config vlan member add 1002 Ethernet4 -u + config interface ip add Vlan1002 fd00:1234:5678:f2:5::/80 + config vlan dhcp_relay add 1002 fd00:1234:5678:3201::1:547 + + # Enable IPv6 link-local + config ipv6 enable link-local + + # Save running config + config save -y + + # Update FRR (BGP) configuration + cat </etc/sonic/frr/frr.conf +frr version 8.1 +frr defaults traditional +hostname swi1-ab-42.fabric.example.com +log syslog informational +service integrated-vtysh-config + +interface Vlan1001 + no ipv6 nd suppress-ra + ipv6 nd managed-config-flag + ipv6 nd other-config-flag + +interface Vlan1002 + no ipv6 nd suppress-ra + ipv6 nd managed-config-flag + ipv6 nd other-config-flag +router bgp 4211000042 + bgp router-id 1.0.0.42 + no bgp ebgp-requires-policy + no bgp default ipv4-unicast + bgp bestpath as-path multipath-relax + no bgp network import-check + + neighbor FABRIC peer-group + neighbor FABRIC remote-as external + neighbor FABRIC timers 3 9 + neighbor FABRIC timers connect 20 + neighbor Ethernet108 interface peer-group FABRIC + neighbor Ethernet112 interface peer-group FABRIC + + neighbor SOUTH peer-group + neighbor SOUTH remote-as external + neighbor SOUTH timers 3 9 + neighbor SOUTH timers connect 20 + neighbor Vlan1001 interface peer-group SOUTH + neighbor Vlan1002 interface peer-group SOUTH + + address-family ipv6 unicast + network fd00:1234:5678:f2::/64 + + neighbor FABRIC activate + neighbor FABRIC route-map RM_FABRIC_IN in + neighbor FABRIC route-map RM_FABRIC_OUT out + + neighbor SOUTH activate + neighbor SOUTH route-map RM_SOUTH_IN in + neighbor SOUTH route-map RM_SOUTH_OUT out + exit-address-family +exit + +route-map RM_FABRIC_IN permit 10 + set community 65000:100 +! +bgp community-list 10 permit 65000:100 + +route-map RM_FABRIC_OUT deny 10 + match community 10 +route-map RM_FABRIC_OUT permit 20 +! +route-map RM_SOUTH_IN permit 10 +route-map RM_SOUTH_OUT permit 10 +! +ipv6 route fd00:1234:5678:f2::/64 reject +EOF + + # Restart BGP to load new config + systemctl restart bgp + + # Mark the cell as applied + touch "$FLAG_FILE_DONE" + reboot +fi +exit 0 diff --git a/sonic/testdata/reset.golden.sh b/sonic/testdata/reset.golden.sh new file mode 100644 index 0000000..4784af2 --- /dev/null +++ b/sonic/testdata/reset.golden.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors +# SPDX-License-Identifier: Apache-2.0 +set -e + +# When run inside a container, re-exec in the host namespaces so the +# config/systemctl/reboot calls below act on the host. +if [ -z "$FABRIC_ON_HOST" ]; then + export FABRIC_ON_HOST=1 + exec nsenter --target 1 --mount --net --pid bash "$0" +fi + +# Reset script for node swi1-ab-42, rendered by the fabric cell runtime. +# Undoes what the cell script applied: server VLANs, loopback IPs, port +# state and the FRR/BGP config. Everything is read from the running +# CONFIG_DB, so this also works on a partially provisioned device (failed +# cells). Deliberately kept: hostname, management config, link-local and +# the physical port parameters (mtu/fec/speed). + +CONFIG_DB=/etc/sonic/config_db.json + +# Remove the loopback addresses handed down by the cell +for ENTRY in $(jq -r '.LOOPBACK_INTERFACE // {} | keys[] | select(contains("|"))' "$CONFIG_DB"); do + config interface ip remove "${ENTRY%%|*}" "${ENTRY#*|}" +done + +# Remove the VLANs the cell's server peers created, in dependency order: +# DHCP relay, interface IPs, members, then the VLAN itself +for VLAN in $(jq -r '.VLAN // {} | keys[]' "$CONFIG_DB"); do + VLAN_ID="${VLAN#Vlan}" + for SERVER in $(jq -r --arg v "$VLAN" '.DHCP_RELAY[$v] // {} | .dhcpv6_servers // .dhcp_servers // [] | .[]' "$CONFIG_DB"); do + config vlan dhcp_relay del "$VLAN_ID" "$SERVER" + done + for ENTRY in $(jq -r --arg v "$VLAN" '.VLAN_INTERFACE // {} | keys[] | select(startswith($v + "|"))' "$CONFIG_DB"); do + config interface ip remove "$VLAN" "${ENTRY#*|}" + done + for MEMBER in $(jq -r --arg v "$VLAN" '.VLAN_MEMBER // {} | keys[] | select(startswith($v + "|"))' "$CONFIG_DB"); do + config vlan member del "$VLAN_ID" "${MEMBER#*|}" + done + config vlan del "$VLAN_ID" +done + +# Shut down every front panel port the cell script started up +for PORT in $(jq -r '.PORT // {} | to_entries[] | select(.value.admin_status == "up") | .key' "$CONFIG_DB"); do + config interface shutdown "$PORT" +done + +config save -y + +# Return FRR to an empty config: no BGP peering +cat </etc/sonic/frr/frr.conf +frr version 8.1 +frr defaults traditional +hostname $(hostname) +log syslog informational +service integrated-vtysh-config +EOF + +systemctl restart bgp + +# Mark the device as unprovisioned, let the fabriclet expire the cell +rm -f /etc/sonic/fabric-cell.done + +reboot diff --git a/sonic/testdata/spine.golden.sh b/sonic/testdata/spine.golden.sh new file mode 100644 index 0000000..31eec52 --- /dev/null +++ b/sonic/testdata/spine.golden.sh @@ -0,0 +1,83 @@ +#!/bin/bash +# SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors +# SPDX-License-Identifier: Apache-2.0 +set -e + +# When run inside a container, re-exec in the host namespaces so the +# config/systemctl/reboot calls below act on the host. +if [ -z "$FABRIC_ON_HOST" ]; then + export FABRIC_ON_HOST=1 + exec nsenter --target 1 --mount --net --pid bash "$0" +fi + +# Flag file to track completion +FLAG_FILE_DONE="/etc/sonic/fabric-cell.done" + +if [ ! -f "$FLAG_FILE_DONE" ]; then + # Configure the loopback addresses handed down by the cell + config interface ip add Loopback0 fd00:1234:5678:f2::/128 + + # Configure the physical settings of all peer ports named by the cell + config interface mtu Ethernet108 9100 + config interface fec Ethernet108 rs + config interface speed Ethernet108 100000 + config interface startup Ethernet108 + config interface mtu Ethernet112 9100 + config interface fec Ethernet112 rs + config interface speed Ethernet112 100000 + config interface startup Ethernet112 + + # Configure the south-facing server ports: one VLAN per port with its + # interface prefix and DHCP relay + + # Enable IPv6 link-local + config ipv6 enable link-local + + # Save running config + config save -y + + # Update FRR (BGP) configuration + cat </etc/sonic/frr/frr.conf +frr version 8.1 +frr defaults traditional +hostname swi2-ab-7.fabric.example.com +log syslog informational +service integrated-vtysh-config +router bgp 4212000042 + bgp router-id 2.0.0.42 + no bgp ebgp-requires-policy + no bgp default ipv4-unicast + bgp bestpath as-path multipath-relax + no bgp network import-check + + neighbor FABRIC peer-group + neighbor FABRIC remote-as external + neighbor FABRIC timers 3 9 + neighbor FABRIC timers connect 20 + neighbor Ethernet108 interface peer-group FABRIC + neighbor Ethernet112 interface peer-group FABRIC + + address-family ipv6 unicast + network fd00:1234:5678:f2::/64 + + neighbor FABRIC activate + neighbor FABRIC route-map RM_FABRIC_IN in + neighbor FABRIC route-map RM_FABRIC_OUT out + + exit-address-family +exit + +route-map RM_FABRIC_IN permit 10 +route-map RM_FABRIC_OUT permit 10 +! +ipv6 route fd00:1234:5678:f2::/64 reject +EOF + + # Restart BGP to load new config + systemctl restart bgp + + # Mark the cell as applied + touch "$FLAG_FILE_DONE" + reboot +fi +exit 0