-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
351 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.