Skip to content

Commit

Permalink
add yurt-lb-agent
Browse files Browse the repository at this point in the history
Signed-off-by: Rui-Gan <[email protected]>
  • Loading branch information
Rui-Gan committed Apr 2, 2024
1 parent 0c62a30 commit 7aa41b3
Show file tree
Hide file tree
Showing 9 changed files with 907 additions and 0 deletions.
173 changes: 173 additions & 0 deletions cmd/yurt-lb-agent/app/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
Copyright 2024 The OpenYurt Authors.
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 app

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
"k8s.io/kubernetes/pkg/util/sysctl"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"

"github.com/openyurtio/openyurt/cmd/yurt-lb-agent/app/options"
"github.com/openyurtio/openyurt/pkg/apis"
"github.com/openyurtio/openyurt/pkg/yurtlbagent/controllers"
"github.com/openyurtio/openyurt/pkg/yurtlbagent/util"
)

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

func init() {
_ = clientgoscheme.AddToScheme(scheme)

_ = apis.AddToScheme(clientgoscheme.Scheme)
_ = apis.AddToScheme(scheme)

// +kubebuilder:scaffold:scheme
}

func NewCmdYurtLBAgent(stopCh <-chan struct{}) *cobra.Command {
yurtLBAgentOptions := options.NewYurtLBAgentOptions()
cmd := &cobra.Command{
Use: "yurt-lb-agent",
Short: "Launch yurt-lb-agent",
Long: "Launch yurt-lb-agent",
Run: func(cmd *cobra.Command, args []string) {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
klog.V(1).Infof("FLAG: --%s=%q", flag.Name, flag.Value)
})
if err := options.ValidateOptions(yurtLBAgentOptions); err != nil {
klog.Fatalf("validate options: %v", err)
}
Run(yurtLBAgentOptions, stopCh)
},
}

yurtLBAgentOptions.AddFlags(cmd.Flags())
return cmd
}

func Run(opts *options.YurtLBAgentOptions, stopCh <-chan struct{}) {
ctrl.SetLogger(klogr.New())
cfg := ctrl.GetConfigOrDie()

mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: opts.MetricsAddr,
HealthProbeBindAddress: opts.ProbeAddr,
LeaderElection: opts.EnableLeaderElection,
LeaderElectionID: "yurt-lb-agent",
Namespace: opts.Namespace,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}

// perform preflight check
setupLog.Info("[preflight] Running pre-flight checks")
if err := preflightCheck(mgr, opts); err != nil {
setupLog.Error(err, "could not run pre-flight checks")
os.Exit(1)
}
setupLog.Info("yurtlb options: ", fmt.Sprintf("%v", opts))

// get nodepool where yurt-lb-agent run
if opts.Nodepool == "" {
opts.Nodepool, err = util.GetNodePool(mgr.GetConfig(), opts.Node)
if err != nil {
setupLog.Error(err, "could not get the nodepool where yurt-lb-agent run")
os.Exit(1)
}
}
setupLog.Info("yurtlb nodepool ", fmt.Sprintf("%v", opts.Nodepool))

// get interface
if opts.Iface == "" {
opts.Iface, err = util.GetNodeInterface()
if err != nil {
setupLog.Error(err, "could not get valid interface")
os.Exit(1)
}
} else {
err = util.ValidInterface(opts.Iface)
if err != nil {
setupLog.Error(err, "interface is not valid")
os.Exit(1)
}
}
// change the required network setting in /proc
sys := sysctl.New()
err = sys.SetSysctl("net/ipv4/ip_nonlocal_bind", 1)
if err != nil {
setupLog.Error(err, "unable to change network setting", "controller", "Yurt-lb-agent")
os.Exit(1)
}

// setup the PoolService Reconciler
if err = (&controllers.PoolServiceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr, opts); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Yurt-lb-agent")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

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

setupLog.Info("[run controllers] Starting manager, acting on " + fmt.Sprintf("[Nodepool: %s, Namespace: %s]", opts.Nodepool, opts.Namespace))
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "could not running manager")
os.Exit(1)
}
}

func preflightCheck(mgr ctrl.Manager, opts *options.YurtLBAgentOptions) error {
client, err := kubernetes.NewForConfig(mgr.GetConfig())
if err != nil {
return err
}
if _, err := client.CoreV1().Namespaces().Get(context.TODO(), opts.Namespace, metav1.GetOptions{}); err != nil {
return err
}
if _, err := client.CoreV1().Nodes().Get(context.TODO(), opts.Node, metav1.GetOptions{}); err != nil {
return fmt.Errorf("failed to get node %v", opts.Node)
}

return nil
}
65 changes: 65 additions & 0 deletions cmd/yurt-lb-agent/app/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2024 The OpenYurt Authors.
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 options

import (
"fmt"

"github.com/spf13/pflag"
)

// YurtIoTDockOptions is the main settings for the yurt-iot-dock
type YurtLBAgentOptions struct {
MetricsAddr string
ProbeAddr string
EnableLeaderElection bool
Namespace string
Version string
Nodepool string
Iface string
Node string
}

func NewYurtLBAgentOptions() *YurtLBAgentOptions {
return &YurtLBAgentOptions{
MetricsAddr: ":8080",
ProbeAddr: ":8080",
EnableLeaderElection: false,
Namespace: "default",
Version: "",
Nodepool: "",
Node: "",
}
}

func ValidateOptions(options *YurtLBAgentOptions) error {
if options.Node == "" {
return fmt.Errorf("node name is required")
}
return nil
}

func (o *YurtLBAgentOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.MetricsAddr, "metrics-bind-address", o.MetricsAddr, "The address the metric endpoint binds to.")
fs.StringVar(&o.ProbeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
fs.BoolVar(&o.EnableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+"Enabling this will ensure there is only one active controller manager.")
fs.StringVar(&o.Namespace, "namespace", "", "The namespace YurtLB Agent is deployed in.(just for debugging)")
fs.StringVar(&o.Version, "version", "", "The version of edge resources deploymenet.")
fs.StringVar(&o.Nodepool, "nodepool", "", "The nodePool YurtLB Agent is deployed in.(just for debugging)")
fs.StringVar(&o.Iface, "iface", "", "The interface keepalived used")
fs.StringVar(&o.Node, "node", "", "The node YurtLB Agent is deployed in.")
}
43 changes: 43 additions & 0 deletions cmd/yurt-lb-agent/yurt-lb-agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2024 The OpenYurt Authors.
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 (
"flag"
"math/rand"
"time"

"k8s.io/apimachinery/pkg/util/wait"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/klog/v2"

"github.com/openyurtio/openyurt/cmd/yurt-lb-agent/app"
)

func main() {
rand.Seed(time.Now().UnixNano())
klog.InitFlags(nil)
defer klog.Flush()

cmd := app.NewCmdYurtLBAgent(wait.NeverStop)
cmd.Flags().AddGoFlagSet(flag.CommandLine)
if err := cmd.Execute(); err != nil {
panic(err)
}
}
8 changes: 8 additions & 0 deletions hack/dockerfiles/build/Dockerfile.yurt-lb-agent
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# multi-arch image building for yurt-lb-agent

FROM --platform=${TARGETPLATFORM} alpine:3.17
ARG TARGETOS TARGETARCH MIRROR_REPO
RUN if [ ! -z "${MIRROR_REPO+x}" ]; then sed -i "s/dl-cdn.alpinelinux.org/${MIRROR_REPO}/g" /etc/apk/repositories; fi && \
apk add ca-certificates bash libc6-compat iptables ip6tables busybox-binsh keepalived-common libcrypto3 libgcc libnl3 libssl3 musl keepalived && update-ca-certificates && rm /var/cache/apk/*
COPY ./_output/local/bin/${TARGETOS}/${TARGETARCH}/yurt-lb-agent /usr/local/bin/yurt-lb-agent
ENTRYPOINT ["/usr/local/bin/yurt-lb-agent"]
1 change: 1 addition & 0 deletions hack/make-rules/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ readonly YURT_ALL_TARGETS=(
yurthub
yurt-manager
yurt-iot-dock
yurt-lb-agent
)

# clean old binaries at GOOS and GOARCH
Expand Down
Loading

0 comments on commit 7aa41b3

Please sign in to comment.