2021-08-10 20:22:40 +00:00
package cmd
import (
"github.com/jet/kube-webhook-certgen/pkg/k8s"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
admissionv1 "k8s.io/api/admissionregistration/v1"
)
2021-09-16 20:59:26 +00:00
var patch = & cobra . Command {
Use : "patch" ,
Short : "Patch a validatingwebhookconfiguration and mutatingwebhookconfiguration 'webhook-name' by using the ca from 'secret-name' in 'namespace'" ,
Long : "Patch a validatingwebhookconfiguration and mutatingwebhookconfiguration 'webhook-name' by using the ca from 'secret-name' in 'namespace'" ,
PreRun : prePatchCommand ,
Run : patchCommand ,
}
2021-08-10 20:22:40 +00:00
func prePatchCommand ( cmd * cobra . Command , args [ ] string ) {
configureLogging ( cmd , args )
2021-09-16 20:59:26 +00:00
if ! cfg . patchMutating && ! cfg . patchValidating {
2021-08-10 20:22:40 +00:00
log . Fatal ( "patch-validating=false, patch-mutating=false. You must patch at least one kind of webhook, otherwise this command is a no-op" )
}
switch cfg . patchFailurePolicy {
case "" :
break
case "Ignore" :
case "Fail" :
failurePolicy = admissionv1 . FailurePolicyType ( cfg . patchFailurePolicy )
break
default :
log . Fatalf ( "patch-failure-policy %s is not valid" , cfg . patchFailurePolicy )
}
}
func patchCommand ( _ * cobra . Command , _ [ ] string ) {
2021-09-16 20:59:26 +00:00
k := k8s . New ( newKubernetesClient ( cfg . kubeconfig ) )
2021-08-10 20:22:40 +00:00
ca := k . GetCaFromSecret ( cfg . secretName , cfg . namespace )
if ca == nil {
log . Fatalf ( "no secret with '%s' in '%s'" , cfg . secretName , cfg . namespace )
}
k . PatchWebhookConfigurations ( cfg . webhookName , ca , & failurePolicy , cfg . patchMutating , cfg . patchValidating )
}
func init ( ) {
rootCmd . AddCommand ( patch )
patch . Flags ( ) . StringVar ( & cfg . secretName , "secret-name" , "" , "Name of the secret where certificate information will be read from" )
patch . Flags ( ) . StringVar ( & cfg . namespace , "namespace" , "" , "Namespace of the secret where certificate information will be read from" )
patch . Flags ( ) . StringVar ( & cfg . webhookName , "webhook-name" , "" , "Name of validatingwebhookconfiguration and mutatingwebhookconfiguration that will be updated" )
patch . Flags ( ) . BoolVar ( & cfg . patchValidating , "patch-validating" , true , "If true, patch validatingwebhookconfiguration" )
patch . Flags ( ) . BoolVar ( & cfg . patchMutating , "patch-mutating" , true , "If true, patch mutatingwebhookconfiguration" )
patch . Flags ( ) . StringVar ( & cfg . patchFailurePolicy , "patch-failure-policy" , "" , "If set, patch the webhooks with this failure policy. Valid options are Ignore or Fail" )
patch . MarkFlagRequired ( "secret-name" )
patch . MarkFlagRequired ( "namespace" )
patch . MarkFlagRequired ( "webhook-name" )
}