Skip to content

Instantly share code, notes, and snippets.

@electrocucaracha
Created September 10, 2020 20:24
Show Gist options
  • Save electrocucaracha/a2db9367c460a4cbe4466c3266e3b33f to your computer and use it in GitHub Desktop.
Save electrocucaracha/a2db9367c460a4cbe4466c3266e3b33f to your computer and use it in GitHub Desktop.
P-GW NSM implementation
// Copyright 2020
// SPDX-License-Identifier: Apache-2.0
//
// 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"
"github.com/networkservicemesh/networkservicemesh/utils"
"github.com/networkservicemesh/networkservicemesh/pkg/tools"
"github.com/networkservicemesh/networkservicemesh/sdk/common"
"github.com/networkservicemesh/networkservicemesh/sdk/endpoint"
"github.com/sirupsen/logrus"
)
const (
AdvertiseNseNameEnv utils.EnvVar = "ADVERTISE_NSE_NAME"
S5uAdvertiseNseLabelsEnv utils.EnvVar = "S5U_ADVERTISE_NSE_LABELS"
S5uIPAddressEnv utils.EnvVar = "S5U_IP_ADDRESS"
S5cAdvertiseNseLabelsEnv utils.EnvVar = "S5C_ADVERTISE_NSE_LABELS"
S5cIPAddressEnv utils.EnvVar = "S5C_IP_ADDRESS"
SgiAdvertiseNseLabelsEnv utils.EnvVar = "SGI_ADVERTISE_NSE_LABELS"
SgiIPAddressEnv utils.EnvVar = "SGI_IP_ADDRESS"
)
// SingleEndpoint keeps the state of a single endpoint instance
type SingleEndpoint struct {
Cleanup func()
}
func main() {
logrus.Info("Starting nse...")
utils.PrintAllEnv(logrus.StandardLogger())
// Capture signals to cleanup before exiting
c := tools.NewOSSignalChannel()
var configs = []map[string]string{
{
"AdvertiseNseLabels": S5uAdvertiseNseLabelsEnv.GetStringOrDefault("link=s5u"),
"IPAddress": S5uIPAddressEnv.GetStringOrDefault("172.25.0.0/24"),
},
{
"AdvertiseNseLabels": S5cAdvertiseNseLabelsEnv.GetStringOrDefault("link=s5c"),
"IPAddress": S5cIPAddressEnv.GetStringOrDefault("172.25.1.0/24"),
},
{
"AdvertiseNseLabels": SgiAdvertiseNseLabelsEnv.GetStringOrDefault("link=sgi"),
"IPAddress": SgiIPAddressEnv.GetStringOrDefault("10.0.1.0/24"),
},
}
endpoints := []*SingleEndpoint{}
for _, config := range configs {
configuration := common.FromEnv()
configuration.AdvertiseNseLabels = config["AdvertiseNseLabels"]
configuration.IPAddress = config["IPAddress"]
logrus.Info("configuration.AdvertiseNseLabels:" + configuration.AdvertiseNseLabels)
logrus.Info("configuration.IPAddress:" + configuration.IPAddress)
podName := endpoint.CreatePodNameMutator()
composite := endpoint.NewCompositeEndpoint(
endpoint.NewMonitorEndpoint(configuration),
endpoint.NewConnectionEndpoint(configuration),
endpoint.NewIpamEndpoint(configuration),
endpoint.NewCustomFuncEndpoint("podName", podName),
)
nsEndpoint, err := endpoint.NewNSMEndpoint(context.Background(), configuration, composite)
if err != nil {
logrus.Fatalf("%v", err)
}
if err := nsEndpoint.Start(); err != nil {
logrus.Fatalf("Unable to start the endpoint: %v", err)
}
endpoints = append(endpoints, &SingleEndpoint{
Cleanup: func() { _ = nsEndpoint.Delete() },
})
}
defer func() {
for _, e := range endpoints {
e.Cleanup()
}
}()
// Capture signals to cleanup before exiting
<-c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment