Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSD-24275: Validate machineCIDR is contained in default ingresscontro… #318

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
60 changes: 60 additions & 0 deletions build/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const (
roleName string = "validation-webhook"
prometheusRoleName string = "prometheus-k8s"
repoName string = "managed-cluster-validating-webhooks"
// Role and Binding for reading cluster-config-v1 config map...
clusterConfigRole string = "config-v1-reader-wh"
clusterConfigRoleBinding string = "validation-webhook-cluster-config-v1-reader"
// Used to define what phase a resource should be deployed in by package-operator
pkoPhaseAnnotation string = "package-operator.run/phase"
// Defines the 'rbac' package-operator phase for any resources related to RBAC
Expand Down Expand Up @@ -211,6 +214,60 @@ func createClusterRoleBinding() *rbacv1.ClusterRoleBinding {
}
}

func createClusterConfigRole() *rbacv1.Role {
return &rbacv1.Role{
TypeMeta: metav1.TypeMeta{
Kind: "Role",
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: clusterConfigRole,
Namespace: "kube-system",
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{
"",
},
Resources: []string{
"configmaps",
},
Verbs: []string{
"get",
},
ResourceNames: []string{
"cluster-config-v1",
},
},
},
}
}

func createClusterConfigRoleBinding() *rbacv1.RoleBinding {
return &rbacv1.RoleBinding{
TypeMeta: metav1.TypeMeta{
Kind: "RoleBinding",
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: clusterConfigRoleBinding,
Namespace: "kube-system",
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: serviceAccountName,
Namespace: *namespace,
},
},
RoleRef: rbacv1.RoleRef{
Name: clusterConfigRole,
Kind: "Role",
APIGroup: rbacv1.GroupName,
},
}
}

func createPrometheusRole() *rbacv1.Role {
return &rbacv1.Role{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -827,6 +884,7 @@ func sliceContains(needle string, haystack []string) bool {

func main() {
flag.Parse()
utils.BuildRun = true

skip := strings.Split(*excludes, ",")
onlyInclude := strings.Split(*only, "")
Expand All @@ -851,6 +909,8 @@ func main() {
templateResources.Add(utils.DefaultLabelSelector(), runtime.RawExtension{Object: createClusterRoleBinding()})
templateResources.Add(utils.DefaultLabelSelector(), runtime.RawExtension{Object: createPrometheusRole()})
templateResources.Add(utils.DefaultLabelSelector(), runtime.RawExtension{Object: createPromethusRoleBinding()})
templateResources.Add(utils.DefaultLabelSelector(), runtime.RawExtension{Object: createClusterConfigRole()})
templateResources.Add(utils.DefaultLabelSelector(), runtime.RawExtension{Object: createClusterConfigRoleBinding()})
templateResources.Add(utils.DefaultLabelSelector(), runtime.RawExtension{Object: createServiceMonitor()})
templateResources.Add(utils.DefaultLabelSelector(), runtime.RawExtension{Object: createCACertConfigMap()})
templateResources.Add(utils.DefaultLabelSelector(), runtime.RawExtension{Object: createService()})
Expand Down
29 changes: 29 additions & 0 deletions build/selectorsyncset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,35 @@ objects:
- kind: ServiceAccount
name: prometheus-k8s
namespace: openshift-monitoring
- apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: null
name: config-v1-reader-wh
namespace: kube-system
rules:
- apiGroups:
- ""
resourceNames:
- cluster-config-v1
resources:
- configmaps
verbs:
- get
- apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
creationTimestamp: null
name: validation-webhook-cluster-config-v1-reader
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: config-v1-reader-wh
subjects:
- kind: ServiceAccount
name: validation-webhook
namespace: openshift-validation-webhook
- apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
Expand Down
18 changes: 11 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
"github.com/openshift/managed-cluster-validating-webhooks/pkg/k8sutil"
"github.com/openshift/managed-cluster-validating-webhooks/pkg/localmetrics"
"github.com/openshift/managed-cluster-validating-webhooks/pkg/webhooks"
"github.com/openshift/managed-cluster-validating-webhooks/pkg/webhooks/utils"
)

var log = logf.Log.WithName("handler")

var (
listenAddress = flag.String("listen", "0.0.0.0", "listen address")
listenPort = flag.String("port", "5000", "port to listen on")
testHooks = flag.Bool("testhooks", false, "Test webhook URI uniqueness and quit?")
metricsAddr string

useTLS = flag.Bool("tls", false, "Use TLS? Must specify -tlskey, -tlscert, -cacert")
tlsKey = flag.String("tlskey", "", "TLS Key for TLS")
Expand All @@ -39,15 +40,18 @@ var (
metricsPort = "8080"
)

func main() {
var metricsAddr string
func init() {
// Allow export webhook var to share flag value...
flag.BoolVar(&utils.TestHooks, "testhooks", false, "Test webhook URI uniqueness and quit?")
flag.StringVar(&metricsAddr, "metrics-bind-address", ":"+metricsPort, "The address the metric endpoint binds to.")
flag.Parse()
klog.SetOutput(os.Stdout)
}

func main() {
klog.SetOutput(os.Stdout)
logf.SetLogger(klogr.New())

if !*testHooks {
if !utils.TestHooks {
log.Info("HTTP server running at", "listen", net.JoinHostPort(*listenAddress, *listenPort))
}
dispatcher := dispatcher.NewDispatcher(webhooks.Webhooks)
Expand All @@ -58,12 +62,12 @@ func main() {
panic(fmt.Errorf("Duplicate webhook trying to listen on %s", realHook.GetURI()))
}
seen[name] = true
if !*testHooks {
if !utils.TestHooks {
log.Info("Listening", "webhookName", name, "URI", realHook.GetURI())
}
http.HandleFunc(realHook.GetURI(), dispatcher.HandleRequest)
}
if *testHooks {
if utils.TestHooks {
os.Exit(0)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
github.com/onsi/gomega v1.29.0 // indirect
github.com/openshift/custom-resource-status v1.1.3-0.20220503160415-f2fdb4999d87 // indirect
github.com/openshift/elasticsearch-operator v0.0.0-20220613183908-e1648e67c298 // indirect
github.com/openshift/installer v0.16.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ github.com/openshift/elasticsearch-operator v0.0.0-20220613183908-e1648e67c298 h
github.com/openshift/elasticsearch-operator v0.0.0-20220613183908-e1648e67c298/go.mod h1:6dxhWPY3Wr/0b0eGrFpV7gcyeS+ne48Mo9OQ9dxrLNI=
github.com/openshift/hive/apis v0.0.0-20230327212335-7fd70848a6d5 h1:adHXZ1WFqCvXpargpTa6divneeUuvV2xr/D6NWgbqS8=
github.com/openshift/hive/apis v0.0.0-20230327212335-7fd70848a6d5/go.mod h1:VIxA5HhvBmsqVn7aUVQYs004B9K4U5A+HrFwvRq2nK8=
github.com/openshift/installer v0.16.1 h1:PmjALN9x1NVNVi3SCqfz0ZwVCgOkQLQWo2nHYXREq/A=
github.com/openshift/installer v0.16.1/go.mod h1:VWGgpJgF8DGCKQjbccnigglhZnHtRLCZ6cxqkXN4Ck0=
github.com/openshift/operator-custom-metrics v0.5.1 h1:1pk4YMUV+cmqfV0f2fyxY62cl7Gc76kwudJT+EdcfYM=
github.com/openshift/operator-custom-metrics v0.5.1/go.mod h1:0dYDHi/ubKRWzsC9MmW6bRMdBgo1QSOuAh3GupTe0Sw=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
Loading