Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network Manager #116

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rbacs: controller-gen
$(CONTROLLER_GEN) paths="./pkg/local-resource-manager" rbac:roleName=node-local-resource-manager output:rbac:stdout | awk -v RS="---\n" 'NR>1{f="./deployments/node/files/node-local-resource-manager-" $$4 ".yaml";printf "%s",$$0 > f; close(f)}' && $(SED_COMMAND) deployments/node/files/node-local-resource-manager-ClusterRole.yaml
$(CONTROLLER_GEN) paths="./pkg/rear-manager/" rbac:roleName=node-rear-manager output:rbac:stdout | awk -v RS="---\n" 'NR>1{f="./deployments/node/files/node-rear-manager-" $$4 ".yaml";printf "%s",$$0 > f; close(f)}' && $(SED_COMMAND) deployments/node/files/node-rear-manager-ClusterRole.yaml
$(CONTROLLER_GEN) paths="./pkg/rear-controller/..." rbac:roleName=node-rear-controller output:rbac:stdout | awk -v RS="---\n" 'NR>1{f="./deployments/node/files/node-rear-controller-" $$4 ".yaml";printf "%s",$$0 > f; close(f)}' && $(SED_COMMAND) deployments/node/files/node-rear-controller-ClusterRole.yaml
$(CONTROLLER_GEN) paths="./pkg/network-manager/" rbac:roleName=node-network-manager output:rbac:stdout | awk -v RS="---\n" 'NR>1{f="./deployments/node/files/node-network-manager-" $$4 ".yaml";printf "%s",$$0 > f; close(f)}' && $(SED_COMMAND) deployments/node/files/node-network-manager-ClusterRole.yaml

# Install gci if not available
gci:
Expand Down
34 changes: 34 additions & 0 deletions apis/network/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2022-2024 FLUIDOS Project
//
// 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 contains API Schema definitions for the advertisement v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=network.fluidos.eu
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects.
GroupVersion = schema.GroupVersion{Group: "network.fluidos.eu", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
23 changes: 23 additions & 0 deletions apis/network/v1alpha1/knowncluster_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2022-2024 FLUIDOS Project
//
// 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 "github.com/fluidos-project/node/pkg/utils/tools"

// UpdateStatus updates the status of the KnownCluster.
func (knowncluster *KnownCluster) UpdateStatus() {
knowncluster.Status.LastUpdateTime = tools.GetTimeNow()
knowncluster.Status.ExpirationTime = tools.GetExpirationTime(0, 0, 10)
}
62 changes: 62 additions & 0 deletions apis/network/v1alpha1/knowncluster_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2022-2024 FLUIDOS Project
//
// 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 (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// KnownClusterSpec defines the desired state of KnownCluster.
type KnownClusterSpec struct {

// Address of the KnownCluster.
Address string `json:"address"`
}

// KnownClusterStatus defines the observed state of KnownCluster.
type KnownClusterStatus struct {

// This field represents the expiration time of the KnownCluster. It is used to determine when the KnownCluster is no longer valid.
ExpirationTime string `json:"expirationTime"`

// This field represents the last update time of the KnownCluster.
LastUpdateTime string `json:"lastUpdateTime"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:shortName=kclust;kclusts

// KnownCluster is the Schema for the clusters API.
type KnownCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec KnownClusterSpec `json:"spec,omitempty"`
Status KnownClusterStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&KnownCluster{}, &KnownClusterList{})
}
112 changes: 112 additions & 0 deletions apis/network/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions cmd/network-manager/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2022-2024 FLUIDOS Project
//
// 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 main is the entrypoint for the network manager
package main
129 changes: 129 additions & 0 deletions cmd/network-manager/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2022-2024 FLUIDOS Project
//
// 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 main

import (
"context"
"flag"
"os"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

networkv1alpha1 "github.com/fluidos-project/node/apis/network/v1alpha1"
nodecorev1alpha1 "github.com/fluidos-project/node/apis/nodecore/v1alpha1"
networkmanager "github.com/fluidos-project/node/pkg/network-manager"
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)

func init() {
utilruntime.Must(corev1.AddToScheme(scheme))
utilruntime.Must(nodecorev1alpha1.AddToScheme(scheme))
utilruntime.Must(networkv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
enableLocalDiscovery := flag.Bool("enable-local-discovery", true, "Enable discovery of other clusters on same LAN")
cniInterface := flag.String("cniInterface", "", "Name of the CNI virtual interface")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

// Client
cfg := ctrl.GetConfigOrDie()
cl, err := client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
setupLog.Error(err, "Unable to create client")
os.Exit(1)
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "a0b0c1d1.fluidos.eu",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}

// Print something about the mgr
setupLog.Info("Manager started", "manager", mgr)

// Buffer for clusters multicast messages
nm := &networkmanager.NetworkManager{EnableLocalDiscovery: *enableLocalDiscovery}

// Start the NetworkManager setup
if err := networkmanager.Setup(context.Background(), cl, nm, cniInterface); err != nil {
setupLog.Error(err, "Unable to setup NetworkManager")
os.Exit(1)
}

// Start the NetworkManager extecution
if err := networkmanager.Execute(context.Background(), cl, nm); err != nil {
setupLog.Error(err, "Unable to execute NetworkManager")
os.Exit(1)
}

// Register the controller
if err = (&networkmanager.KnownClusterReconciler{
Client: cl,
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "KnownCluster")
os.Exit(1)
}

// Register health checks
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}

// Start the NetworkManager reconcile
setupLog.Info("Starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
Loading
Loading