Skip to content

Commit

Permalink
Fix sanity tests
Browse files Browse the repository at this point in the history
* Remove kube-rbac-proxy.
* Remove component config support.
* Regenerate test-data
  • Loading branch information
Neo2308 committed Aug 9, 2024
1 parent 8ce52da commit 38e793b
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 188 deletions.
10 changes: 2 additions & 8 deletions pkg/plugins/helm/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,10 @@ func addInitCustomizations(projectName string) error {
// todo: we ought to use afero instead. Replace this methods to insert/update
// by https://github.com/kubernetes-sigs/kubebuilder/pull/2119

// Add leader election arg in config/manager/manager.yaml and in config/default/manager_auth_proxy_patch.yaml
// Add leader election arg in config/manager/manager.yaml
err := kbutils.InsertCode(managerFile,
"--leader-elect",
fmt.Sprintf("\n - --leader-election-id=%s", projectName))
if err != nil {
return err
}
err = kbutils.InsertCode(filepath.Join("config", "default", "manager_auth_proxy_patch.yaml"),
"- \"--leader-elect\"",
fmt.Sprintf("\n - \"--leader-election-id=%s\"", projectName))
fmt.Sprintf("\n - --leader-election-id=%s", projectName))
if err != nil {
return err
}
Expand Down
10 changes: 2 additions & 8 deletions pkg/plugins/hybrid/v1alpha/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,10 @@ func addInitCustomizations(projectName string) error {
// todo: we ought to use afero instead. Replace this methods to insert/update
// by https://github.com/kubernetes-sigs/kubebuilder/pull/2119

// Add leader election arg in config/manager/manager.yaml and in config/default/manager_auth_proxy_patch.yaml
// Add leader election arg in config/manager/manager.yaml
err := util.InsertCode(managerFile,
"--leader-elect",
fmt.Sprintf("\n - --leader-election-id=%s", projectName))
if err != nil {
return err
}
err = util.InsertCode(filepath.Join("config", "default", "manager_auth_proxy_patch.yaml"),
"- \"--leader-elect\"",
fmt.Sprintf("\n - \"--leader-election-id=%s\"", projectName))
fmt.Sprintf("\n - --leader-election-id=%s", projectName))
if err != nil {
return err
}
Expand Down
22 changes: 0 additions & 22 deletions pkg/plugins/hybrid/v1alpha/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ func init() {
}
func main() {
{{- if not .ComponentConfig }}
var (
metricsAddr string
leaderElectionID string
Expand All @@ -248,13 +247,6 @@ func main() {
"Whether or not the metrics endpoint should be served securely")
flag.BoolVar(&enableHTTP2, "enable-http2", false,
"Whether or not HTTP/2 should be enabled for the metrics and webhook servers")
{{- else }}
var configFile string
flag.StringVar(&configFile, "config", "",
"The controller will load its initial configuration from this file. " +
"Omit this flag to use the default configuration values. " +
"Command-line flags override configuration from this file.")
{{- end }}
opts := zap.Options{
Development: true,
}
Expand All @@ -263,7 +255,6 @@ func main() {
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
{{ if not .ComponentConfig }}
disableHTTP2 := func(c *tls.Config) {
setupLog.Info("disabling http/2")
c.NextProtos = []string{"http/1.1"}
Expand All @@ -290,19 +281,6 @@ func main() {
LeaderElection: enableLeaderElection,
LeaderElectionID: leaderElectionID,
})
{{- else }}
var err error
options := ctrl.Options{Scheme: scheme}
if configFile != "" {
options, err = options.AndFrom(ctrl.ConfigFile().AtPath(configFile))
if err != nil {
setupLog.Error(err, "unable to load the config file")
os.Exit(1)
}
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
{{- end }}
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions testdata/hybrid/memcached-operator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
KUSTOMIZE_VERSION ?= v5.4.1
CONTROLLER_TOOLS_VERSION ?= v0.15.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type MemcachedStatus struct {
// Important: Run "make" to regenerate code after modifying this file
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// Memcached is the Schema for the memcacheds API
type Memcached struct {
Expand All @@ -50,7 +50,7 @@ type Memcached struct {
Status MemcachedStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
// +kubebuilder:object:root=true

// MemcachedList contains a list of Memcached
type MemcachedList struct {
Expand Down
6 changes: 3 additions & 3 deletions testdata/hybrid/memcached-operator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (

cachev1alpha1 "github.com/example/memcached-operator/api/v1alpha1"
"github.com/example/memcached-operator/internal/controller"
//+kubebuilder:scaffold:imports
// +kubebuilder:scaffold:imports
)

var (
Expand All @@ -54,7 +54,7 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(cachev1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
// +kubebuilder:scaffold:scheme
}

func main() {
Expand Down Expand Up @@ -125,7 +125,7 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Memcached")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
// +kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ resources:
- ../manager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus
# [METRICS] To enable the controller manager metrics service, uncomment the following line.
#- metrics_service.yaml

patches:
# Protect the /metrics endpoint by putting it behind auth.
# If you want your controller-manager to expose the /metrics
# endpoint w/o any authn/z, please comment the following line.
- path: manager_auth_proxy_patch.yaml
# Uncomment the patches line if you enable Metrics, and/or are using webhooks and cert-manager
#patches:
# [METRICS] The following patch will enable the metrics endpoint. Ensure that you also protect this endpoint.
# More info: https://book.kubebuilder.io/reference/metrics
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment
Original file line number Diff line number Diff line change
@@ -1,40 +0,0 @@
# This patch inject a sidecar container which is a HTTP proxy for the
# controller manager, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: kube-rbac-proxy
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- "ALL"
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.16.0
args:
- "--secure-listen-address=0.0.0.0:8443"
- "--upstream=http://127.0.0.1:8080/"
- "--logtostderr=true"
- "--v=0"
ports:
- containerPort: 8443
protocol: TCP
name: https
resources:
limits:
cpu: 500m
memory: 128Mi
requests:
cpu: 5m
memory: 64Mi
- name: manager
args:
- "--health-probe-bind-address=:8081"
- "--metrics-bind-address=127.0.0.1:8080"
- "--leader-elect"
- "--leader-election-id=memcached-operator"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This patch adds the args to allow exposing the metrics endpoint securely
- op: add
path: /spec/template/spec/containers/0/args/0
value: --metrics-bind-address=:8080
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: v1
kind: Service
metadata:
labels:
control-plane: controller-manager
app.kubernetes.io/name: memcached-operator
app.kubernetes.io/managed-by: kustomize
name: controller-manager-metrics-service
namespace: system
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
control-plane: controller-manager
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ spec:
# type: RuntimeDefault
containers:
- args:
- --leader-elect
- --leader-election-id=memcached-operator
- --leader-elect
- --leader-election-id=memcached-operator
- --health-probe-bind-address=:8081
image: controller:latest
name: manager
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ metadata:
spec:
endpoints:
- path: /metrics
port: https
scheme: https
bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
tlsConfig:
insecureSkipVerify: true
port: http # Ensure this is the name of the port that exposes HTTP metrics
scheme: http
selector:
matchLabels:
control-plane: controller-manager
Original file line number Diff line number Diff line change
@@ -1,12 +0,0 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/name: memcached-operator
app.kubernetes.io/managed-by: kustomize
name: metrics-reader
rules:
- nonResourceURLs:
- "/metrics"
verbs:
- get
Original file line number Diff line number Diff line change
@@ -1,20 +0,0 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/name: memcached-operator
app.kubernetes.io/managed-by: kustomize
name: proxy-role
rules:
- apiGroups:
- authentication.k8s.io
resources:
- tokenreviews
verbs:
- create
- apiGroups:
- authorization.k8s.io
resources:
- subjectaccessreviews
verbs:
- create
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
app.kubernetes.io/name: memcached-operator
app.kubernetes.io/managed-by: kustomize
name: proxy-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: proxy-role
subjects:
- kind: ServiceAccount
name: controller-manager
namespace: system
Original file line number Diff line number Diff line change
@@ -1,17 +0,0 @@
apiVersion: v1
kind: Service
metadata:
labels:
control-plane: controller-manager
app.kubernetes.io/name: memcached-operator
app.kubernetes.io/managed-by: kustomize
name: controller-manager-metrics-service
namespace: system
spec:
ports:
- name: https
port: 8443
protocol: TCP
targetPort: https
selector:
control-plane: controller-manager
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,3 @@ resources:
- role_binding.yaml
- leader_election_role.yaml
- leader_election_role_binding.yaml
# Comment the following 4 lines if you want to disable
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
# which protects your /metrics endpoint.
- auth_proxy_service.yaml
- auth_proxy_role.yaml
- auth_proxy_role_binding.yaml
- auth_proxy_client_clusterrole.yaml
1 change: 0 additions & 1 deletion testdata/hybrid/memcached-operator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type MemcachedReconciler struct {
Scheme *runtime.Scheme
}

//+kubebuilder:rbac:groups=cache.my.domain,resources=memcacheds,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=cache.my.domain,resources=memcacheds/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=cache.my.domain,resources=memcacheds/finalizers,verbs=update
// +kubebuilder:rbac:groups=cache.my.domain,resources=memcacheds,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=cache.my.domain,resources=memcacheds/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cache.my.domain,resources=memcacheds/finalizers,verbs=update

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand All @@ -45,7 +45,7 @@ type MemcachedReconciler struct {
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.17.3/pkg/reconcile
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.2/pkg/reconcile
func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)

Expand Down
Loading

0 comments on commit 38e793b

Please sign in to comment.