Skip to content

Commit

Permalink
ok (#15)
Browse files Browse the repository at this point in the history
* ok
  • Loading branch information
jmnote authored Aug 16, 2024
1 parent a9eb5be commit 6f0a2f4
Show file tree
Hide file tree
Showing 22 changed files with 982 additions and 654 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ issues:
- path: "api/*"
linters:
- lll
- path: "internal/*"
- path: "controller/*"
linters:
- dupl
- lll
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ RUN go mod download

# Copy the go source
COPY cmd/main.go cmd/main.go
COPY internal/controller/ internal/controller/
COPY pkg/ pkg/
COPY controller/ controller/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand Down
63 changes: 42 additions & 21 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ package main

import (
"crypto/tls"
"errors"
"flag"
"fmt"
"os"

// 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"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -35,14 +38,15 @@ import (
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/kuoss/ingress-annotator/internal/controller"
"github.com/kuoss/ingress-annotator/pkg/rulesstore"
"github.com/kuoss/ingress-annotator/controller"
"github.com/kuoss/ingress-annotator/controller/rulesstore"
// +kubebuilder:scaffold:imports
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
configMapName = "ingress-annotator"
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)

func init() {
Expand All @@ -52,6 +56,13 @@ func init() {
}

func main() {
if err := run(); err != nil {
setupLog.Error(err, "unable to run the manager")
os.Exit(1)
}
}

func run() error {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
Expand Down Expand Up @@ -82,13 +93,12 @@ func main() {
// Rapid Reset CVEs. For more information see:
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
// - https://github.com/advisories/GHSA-4374-p667-p6c8
disableHTTP2 := func(c *tls.Config) {
setupLog.Info("disabling http/2")
c.NextProtos = []string{"http/1.1"}
}

if !enableHTTP2 {
tlsOpts = append(tlsOpts, disableHTTP2)
tlsOpts = append(tlsOpts, func(c *tls.Config) {
setupLog.Info("disabling http/2")
c.NextProtos = []string{"http/1.1"}
})
}

webhookServer := webhook.NewServer(webhook.Options{
Expand Down Expand Up @@ -139,41 +149,52 @@ func main() {
// LeaderElectionReleaseOnCancel: true,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
return errors.New("unable to start manager")
}

ns, exists := os.LookupEnv("POD_NAMESPACE")
if !exists || ns == "" {
return errors.New("POD_NAMESPACE environment variable is not set or is empty")
}

nn := types.NamespacedName{
Namespace: ns,
Name: configMapName,
}
rulesStore, err := rulesstore.New(mgr.GetClient(), nn)
if err != nil {
setupLog.Error(err, "unable to start rules store")
os.Exit(1)
}

rulesStore := rulesstore.New()
if err = (&controller.ConfigMapReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ConfigNN: nn,
RulesStore: rulesStore,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ConfigMap")
os.Exit(1)
return fmt.Errorf("unable to create ConfigMapReconciler: %w", err)
}
if err = (&controller.IngressReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
RulesStore: rulesStore,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Ingress")
os.Exit(1)
return fmt.Errorf("unable to create IngressReconciler: %w", err)
}
// +kubebuilder:scaffold:builder

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

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
return fmt.Errorf("problem running manager: %w", err)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package controller

import (
"context"
"errors"
"fmt"
"os"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -29,14 +27,14 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kuoss/ingress-annotator/pkg/rulesstore"
"github.com/kuoss/ingress-annotator/controller/rulesstore"
)

// ConfigMapReconciler reconciles a ConfigMap object
type ConfigMapReconciler struct {
client.Client
Scheme *runtime.Scheme
ConfigMeta types.NamespacedName
ConfigNN types.NamespacedName
RulesStore rulesstore.IRulesStore
}

Expand All @@ -46,17 +44,6 @@ type ConfigMapReconciler struct {

// SetupWithManager sets up the controller with the Manager.
func (r *ConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error {
ns, exists := os.LookupEnv("POD_NAMESPACE")
if !exists || ns == "" {
return errors.New("POD_NAMESPACE environment variable is not set or is empty")
}
r.ConfigMeta = types.NamespacedName{
Namespace: ns,
Name: "ingress-annotator-rules",
}
if err := r.updateRulesWithConfigMap(context.Background()); err != nil {
return fmt.Errorf("updateRulesWithConfigMap err: %w", err)
}
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.ConfigMap{}).
Complete(r)
Expand All @@ -72,31 +59,14 @@ func (r *ConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
if req.Namespace == r.ConfigMeta.Namespace && req.Name == r.ConfigMeta.Name {
return r.reconcileNormal(ctx, req)
}
return ctrl.Result{}, nil
}
if req.Namespace == r.ConfigNN.Namespace && req.Name == r.ConfigNN.Name {
logger := log.FromContext(ctx).WithValues("kind", "configmap", "namespace", req.Namespace, "name", req.Name)

func (r *ConfigMapReconciler) reconcileNormal(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx).WithValues("kind", "configmap", "namespace", req.Namespace, "name", req.Name).WithCallDepth(1)

logger.Info("Reconciling ConfigMap")
if err := r.updateRulesWithConfigMap(context.Background()); err != nil {
return ctrl.Result{}, fmt.Errorf("updateRulesWithConfigMap err: %w", err)
logger.Info("Reconciling ConfigMap")
if err := r.RulesStore.UpdateRules(); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to update rules in rules store: %w", err)
}
logger.Info("Successfully reconciled ConfigMap")
}

logger.Info("Successfully reconciled ConfigMap")
return ctrl.Result{}, nil
}

func (r *ConfigMapReconciler) updateRulesWithConfigMap(ctx context.Context) error {
var cm corev1.ConfigMap
if err := r.Get(ctx, r.ConfigMeta, &cm); err != nil {
return fmt.Errorf("getConfigMap err: %w", err)
}
if err := r.RulesStore.UpdateRules(&cm); err != nil {
return fmt.Errorf("failed to update data in rules store: %w", err)
}
return nil
}
Loading

0 comments on commit 6f0a2f4

Please sign in to comment.