sigs.k8s.io/kueue@v0.6.2/pkg/util/cert/cert.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cert
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/go-logr/logr"
    23  	cert "github.com/open-policy-agent/cert-controller/pkg/rotator"
    24  	"k8s.io/apimachinery/pkg/types"
    25  	ctrl "sigs.k8s.io/controller-runtime"
    26  
    27  	config "sigs.k8s.io/kueue/apis/config/v1beta1"
    28  )
    29  
    30  const (
    31  	certDir        = "/tmp/k8s-webhook-server/serving-certs"
    32  	vwcName        = "kueue-validating-webhook-configuration"
    33  	mwcName        = "kueue-mutating-webhook-configuration"
    34  	caName         = "kueue-ca"
    35  	caOrganization = "kueue"
    36  )
    37  
    38  // +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;update
    39  // +kubebuilder:rbac:groups="admissionregistration.k8s.io",resources=mutatingwebhookconfigurations,verbs=get;list;watch;update
    40  // +kubebuilder:rbac:groups="admissionregistration.k8s.io",resources=validatingwebhookconfigurations,verbs=get;list;watch;update
    41  
    42  // ManageCerts creates all certs for webhooks. This function is called from main.go.
    43  func ManageCerts(mgr ctrl.Manager, cfg config.Configuration, setupFinished chan struct{}) error {
    44  	// DNSName is <service name>.<namespace>.svc
    45  	var dnsName = fmt.Sprintf("%s.%s.svc", *cfg.InternalCertManagement.WebhookServiceName, *cfg.Namespace)
    46  
    47  	return cert.AddRotator(mgr, &cert.CertRotator{
    48  		SecretKey: types.NamespacedName{
    49  			Namespace: *cfg.Namespace,
    50  			Name:      *cfg.InternalCertManagement.WebhookSecretName,
    51  		},
    52  		CertDir:        certDir,
    53  		CAName:         caName,
    54  		CAOrganization: caOrganization,
    55  		DNSName:        dnsName,
    56  		IsReady:        setupFinished,
    57  		Webhooks: []cert.WebhookInfo{{
    58  			Type: cert.Validating,
    59  			Name: vwcName,
    60  		}, {
    61  			Type: cert.Mutating,
    62  			Name: mwcName,
    63  		}},
    64  		// When kueue is running in the leader election mode,
    65  		// we expect webhook server will run in primary and secondary instance
    66  		RequireLeaderElection: false,
    67  	})
    68  }
    69  
    70  func WaitForCertsReady(log logr.Logger, certsReady chan struct{}) {
    71  	log.Info("Waiting for certificate generation to complete")
    72  	<-certsReady
    73  	log.Info("Certs ready")
    74  }