Skip to content

Commit

Permalink
Added base setup of a k8s operator
Browse files Browse the repository at this point in the history
  • Loading branch information
SEQUOIIA committed Jan 16, 2025
1 parent 91babb4 commit 00c141f
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 52 deletions.
19 changes: 6 additions & 13 deletions cmds/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package main

import (
"context"
"go.dfds.cloud/bootstrap"
"go.dfds.cloud/orchestrator"
"go.dfds.cloud/ssu-k8s/core/logging"
"go.dfds.cloud/ssu-k8s/feats/api"
"go.dfds.cloud/ssu-k8s/feats/messaging"
"go.dfds.cloud/ssu-k8s/feats/jobs"
"go.dfds.cloud/ssu-k8s/feats/operator"
"go.uber.org/zap"
)

Expand All @@ -26,23 +25,17 @@ func main() {
// setup feats
api.Configure(manager.HttpRouter)

configPrefix := "SSU_K8S_JOB"
manager.Orchestrator.AddJob(configPrefix, orchestrator.NewJob("dummy", func(ctx context.Context) error {
logging.Logger.Info("dummy")
return nil
}), &orchestrator.Schedule{})

manager.Orchestrator.Run()

msgWg := messaging.Init(manager)
jobs.Init(manager.Orchestrator)
//msgWg := messaging.Init(manager)

// run
go operator.InitOperator()
<-manager.Context.Done()
if err := manager.HttpServer.Shutdown(manager.Context); err != nil {
logging.Logger.Info("HTTP Server was unable to shut down gracefully", zap.Error(err))
}

msgWg.Wait()
//msgWg.Wait()

logging.Logger.Info("server shutting down")

Expand Down
17 changes: 17 additions & 0 deletions feats/jobs/jobs.go
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
package jobs

import (
"context"
"go.dfds.cloud/orchestrator"
"go.dfds.cloud/ssu-k8s/core/logging"
)

func Init(orc *orchestrator.Orchestrator) {
configPrefix := "SSU_K8S_JOB"

orc.AddJob(configPrefix, orchestrator.NewJob("dummy", func(ctx context.Context) error {
logging.Logger.Info("dummy")
return nil
}), &orchestrator.Schedule{})

orc.Run()
}
57 changes: 57 additions & 0 deletions feats/operator/controller/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package controller

import (
"context"
"fmt"
"go.dfds.cloud/ssu-k8s/core/logging"
"go.dfds.cloud/ssu-k8s/feats/operator/misc"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type NamespaceReconciler struct {
Client client.Client
Scheme *runtime.Scheme
}

// Reconcile TODO: Add reconcile logic for Capability namespaces
// Reconcile Capability namespaces must be labelled with the key "dfds.cloud/capability"
// Reconcile You can opt out of reconciling a namespace resource by setting a label of "dfds.cloud/reconcile: false"
func (r *NamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
nsObj := &v1.Namespace{}

err := r.Client.Get(ctx, req.NamespacedName, nsObj)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
logging.Logger.Debug("namespace resource not found. Ignoring since object must be deleted", zap.Error(err))
return ctrl.Result{}, nil
}
// Error reading the object - requeue the request.
logging.Logger.Debug("Failed to get namespace", zap.Error(err))
return ctrl.Result{}, err
}

// Not a Capability namespace, skip
if _, ok := nsObj.Labels[misc.LabelCapabilityKey]; !ok {
return ctrl.Result{}, nil
}

fmt.Println("Reconciling Namespace: " + nsObj.Name)
fmt.Println("labels:")
for k, v := range nsObj.Labels {
fmt.Printf(" %s: %s\n", k, v)
}

return ctrl.Result{}, nil
}

func (r *NamespaceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).For(&v1.Namespace{}).Complete(r)
}
8 changes: 8 additions & 0 deletions feats/operator/misc/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package misc

import "fmt"

const labelPrefix = "dfds.cloud"

var LabelCapabilityKey = fmt.Sprintf("%s/capability", labelPrefix)
var LabelReconcileKey = fmt.Sprintf("%s/reconcile", labelPrefix)
59 changes: 59 additions & 0 deletions feats/operator/operator.go
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
package operator

import (
"github.com/go-logr/zapr"
"go.dfds.cloud/ssu-k8s/core/logging"
"go.dfds.cloud/ssu-k8s/feats/operator/controller"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"os"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
)

var (
scheme = runtime.NewScheme()
)

func InitOperator() {
ctrl.SetLogger(zapr.NewLogger(logging.Logger))
utilruntime.Must(v1.AddToScheme(scheme))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
//Metrics: metricsServerOptions,
Metrics: server.Options{
BindAddress: "0", // disables metrics for now
},
LeaderElection: false,
LeaderElectionID: "beb9eb71.my.domain",
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
// speeds up voluntary leader transitions as the new leader don't have to wait
// LeaseDuration time first.
//
// In the default scaffold provided, the program ends immediately after
// the manager stops, so would be fine to enable this option. However,
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
// LeaderElectionReleaseOnCancel: true,
})
if err != nil {
logging.Logger.Fatal("unable to start manager", zap.Error(err))
}

if err = (&controller.NamespaceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
logging.Logger.Error("unable to create controller", zap.Error(err), zap.String("controller", "namespace"))
os.Exit(1)
}

logging.Logger.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
logging.Logger.Error("problem running manager", zap.Error(err))
os.Exit(1)
}
}
64 changes: 53 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,91 @@ go 1.23.4

require (
github.com/gin-gonic/gin v1.9.1
github.com/go-logr/zapr v1.3.0
go.dfds.cloud/bootstrap v0.0.3
go.dfds.cloud/messaging v0.0.1
go.dfds.cloud/orchestrator v0.1.7
go.uber.org/zap v1.27.0
k8s.io/api v0.31.0
k8s.io/apimachinery v0.31.0
sigs.k8s.io/controller-runtime v0.19.4
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/segmentio/kafka-go v0.4.47 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.dfds.cloud/utils v0.1.5 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/client-go v0.31.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

//replace go.dfds.cloud/bootstrap => ../go/bootstrap
Expand Down
Loading

0 comments on commit 00c141f

Please sign in to comment.