Skip to content

Commit 3968de1

Browse files
authored
Network Manager implementation (#116)
1 parent e0f4ca9 commit 3968de1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1226
-227
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ rbacs: controller-gen
3131
$(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
3232
$(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
3333
$(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
34+
$(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
3435

3536
# Install gci if not available
3637
gci:
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022-2024 FLUIDOS Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package v1alpha1 contains API Schema definitions for the advertisement v1alpha1 API group
16+
// +kubebuilder:object:generate=true
17+
// +groupName=network.fluidos.eu
18+
package v1alpha1
19+
20+
import (
21+
"k8s.io/apimachinery/pkg/runtime/schema"
22+
"sigs.k8s.io/controller-runtime/pkg/scheme"
23+
)
24+
25+
var (
26+
// GroupVersion is group version used to register these objects.
27+
GroupVersion = schema.GroupVersion{Group: "network.fluidos.eu", Version: "v1alpha1"}
28+
29+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
30+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
31+
32+
// AddToScheme adds the types in this group-version to the given scheme.
33+
AddToScheme = SchemeBuilder.AddToScheme
34+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2022-2024 FLUIDOS Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package v1alpha1
16+
17+
import "github.com/fluidos-project/node/pkg/utils/tools"
18+
19+
// UpdateStatus updates the status of the KnownCluster.
20+
func (knowncluster *KnownCluster) UpdateStatus() {
21+
knowncluster.Status.LastUpdateTime = tools.GetTimeNow()
22+
knowncluster.Status.ExpirationTime = tools.GetExpirationTime(0, 0, 10)
23+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2022-2024 FLUIDOS Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package v1alpha1
16+
17+
import (
18+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19+
)
20+
21+
// KnownClusterSpec defines the desired state of KnownCluster.
22+
type KnownClusterSpec struct {
23+
24+
// Address of the KnownCluster.
25+
Address string `json:"address"`
26+
}
27+
28+
// KnownClusterStatus defines the observed state of KnownCluster.
29+
type KnownClusterStatus struct {
30+
31+
// This field represents the expiration time of the KnownCluster. It is used to determine when the KnownCluster is no longer valid.
32+
ExpirationTime string `json:"expirationTime"`
33+
34+
// This field represents the last update time of the KnownCluster.
35+
LastUpdateTime string `json:"lastUpdateTime"`
36+
}
37+
38+
//+kubebuilder:object:root=true
39+
//+kubebuilder:subresource:status
40+
//+kubebuilder:resource:shortName=kclust;kclusts
41+
42+
// KnownCluster is the Schema for the clusters API.
43+
type KnownCluster struct {
44+
metav1.TypeMeta `json:",inline"`
45+
metav1.ObjectMeta `json:"metadata,omitempty"`
46+
47+
Spec KnownClusterSpec `json:"spec,omitempty"`
48+
Status KnownClusterStatus `json:"status,omitempty"`
49+
}
50+
51+
//+kubebuilder:object:root=true
52+
53+
// KnownClusterList contains a list of KnownCluster.
54+
type KnownClusterList struct {
55+
metav1.TypeMeta `json:",inline"`
56+
metav1.ListMeta `json:"metadata,omitempty"`
57+
Items []KnownCluster `json:"items"`
58+
}
59+
60+
func init() {
61+
SchemeBuilder.Register(&KnownCluster{}, &KnownClusterList{})
62+
}

apis/network/v1alpha1/zz_generated.deepcopy.go

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/network-manager/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2022-2024 FLUIDOS Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package main is the entrypoint for the network manager
16+
package main

cmd/network-manager/main.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright 2022-2024 FLUIDOS Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"context"
19+
"flag"
20+
"os"
21+
22+
corev1 "k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
25+
ctrl "sigs.k8s.io/controller-runtime"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
"sigs.k8s.io/controller-runtime/pkg/healthz"
28+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
29+
30+
networkv1alpha1 "github.com/fluidos-project/node/apis/network/v1alpha1"
31+
nodecorev1alpha1 "github.com/fluidos-project/node/apis/nodecore/v1alpha1"
32+
networkmanager "github.com/fluidos-project/node/pkg/network-manager"
33+
)
34+
35+
var (
36+
scheme = runtime.NewScheme()
37+
setupLog = ctrl.Log.WithName("setup")
38+
)
39+
40+
func init() {
41+
utilruntime.Must(corev1.AddToScheme(scheme))
42+
utilruntime.Must(nodecorev1alpha1.AddToScheme(scheme))
43+
utilruntime.Must(networkv1alpha1.AddToScheme(scheme))
44+
//+kubebuilder:scaffold:scheme
45+
}
46+
47+
func main() {
48+
var metricsAddr string
49+
var enableLeaderElection bool
50+
var probeAddr string
51+
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
52+
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
53+
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
54+
"Enable leader election for controller manager. "+
55+
"Enabling this will ensure there is only one active controller manager.")
56+
enableLocalDiscovery := flag.Bool("enable-local-discovery", true, "Enable discovery of other clusters on same LAN")
57+
cniInterface := flag.String("cniInterface", "", "Name of the CNI virtual interface")
58+
opts := zap.Options{
59+
Development: true,
60+
}
61+
opts.BindFlags(flag.CommandLine)
62+
flag.Parse()
63+
64+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
65+
66+
// Client
67+
cfg := ctrl.GetConfigOrDie()
68+
cl, err := client.New(cfg, client.Options{Scheme: scheme})
69+
if err != nil {
70+
setupLog.Error(err, "Unable to create client")
71+
os.Exit(1)
72+
}
73+
74+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
75+
Scheme: scheme,
76+
MetricsBindAddress: metricsAddr,
77+
HealthProbeBindAddress: probeAddr,
78+
LeaderElection: enableLeaderElection,
79+
LeaderElectionID: "a0b0c1d1.fluidos.eu",
80+
})
81+
if err != nil {
82+
setupLog.Error(err, "unable to start manager")
83+
os.Exit(1)
84+
}
85+
86+
// Print something about the mgr
87+
setupLog.Info("Manager started", "manager", mgr)
88+
89+
// Buffer for clusters multicast messages
90+
nm := &networkmanager.NetworkManager{EnableLocalDiscovery: *enableLocalDiscovery}
91+
92+
// Start the NetworkManager setup
93+
if err := networkmanager.Setup(context.Background(), cl, nm, cniInterface); err != nil {
94+
setupLog.Error(err, "Unable to setup NetworkManager")
95+
os.Exit(1)
96+
}
97+
98+
// Start the NetworkManager extecution
99+
if err := networkmanager.Execute(context.Background(), cl, nm); err != nil {
100+
setupLog.Error(err, "Unable to execute NetworkManager")
101+
os.Exit(1)
102+
}
103+
104+
// Register the controller
105+
if err = (&networkmanager.KnownClusterReconciler{
106+
Client: cl,
107+
Scheme: mgr.GetScheme(),
108+
}).SetupWithManager(mgr); err != nil {
109+
setupLog.Error(err, "unable to create controller", "controller", "KnownCluster")
110+
os.Exit(1)
111+
}
112+
113+
// Register health checks
114+
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
115+
setupLog.Error(err, "unable to set up health check")
116+
os.Exit(1)
117+
}
118+
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
119+
setupLog.Error(err, "unable to set up ready check")
120+
os.Exit(1)
121+
}
122+
123+
// Start the NetworkManager reconcile
124+
setupLog.Info("Starting manager")
125+
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
126+
setupLog.Error(err, "problem running manager")
127+
os.Exit(1)
128+
}
129+
}

0 commit comments

Comments
 (0)