1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

refactor: add os utils sub package (#3528)

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

Co-authored-by: shuting <shuting@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2022-04-01 08:59:44 +02:00 committed by GitHub
parent 663ad49dca
commit fe0ad3c68f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 57 deletions

View file

@ -127,7 +127,7 @@ func main() {
os.Exit(1)
}
depl, err := client.GetResource("", "Deployment", getKyvernoNameSpace(), config.KyvernoDeploymentName)
depl, err := client.GetResource("", "Deployment", config.KyvernoNamespace, config.KyvernoDeploymentName)
deplHash := ""
if err != nil {
log.Log.Info("failed to fetch deployment '%v': %v", config.KyvernoDeploymentName, err.Error())
@ -136,7 +136,7 @@ func main() {
deplHash = fmt.Sprintf("%v", depl.GetUID())
name := tls.GenerateRootCASecretName(certProps)
secretUnstr, err := client.GetResource("", "Secret", getKyvernoNameSpace(), name)
secretUnstr, err := client.GetResource("", "Secret", config.KyvernoNamespace, name)
if err != nil {
log.Log.Info("failed to fetch secret '%v': %v", name, err.Error())
@ -153,7 +153,7 @@ func main() {
}
name = tls.GenerateTLSPairSecretName(certProps)
secretUnstr, err = client.GetResource("", "Secret", getKyvernoNameSpace(), name)
secretUnstr, err = client.GetResource("", "Secret", config.KyvernoNamespace, name)
if err != nil {
log.Log.Info("failed to fetch secret '%v': %v", name, err.Error())
@ -169,7 +169,7 @@ func main() {
}
}
_, err = kubeClientLeaderElection.CoordinationV1().Leases(getKyvernoNameSpace()).Get(ctx, "kyvernopre-lock", v1.GetOptions{})
_, err = kubeClientLeaderElection.CoordinationV1().Leases(config.KyvernoNamespace).Get(ctx, "kyvernopre-lock", v1.GetOptions{})
if err != nil {
log.Log.Info("Lease 'kyvernopre-lock' not found. Starting clean-up...")
} else {
@ -202,7 +202,7 @@ func main() {
Name: "kyvernopre-lock",
},
}
_, err = kubeClientLeaderElection.CoordinationV1().Leases(getKyvernoNameSpace()).Create(ctx, &lease, v1.CreateOptions{})
_, err = kubeClientLeaderElection.CoordinationV1().Leases(config.KyvernoNamespace).Create(ctx, &lease, v1.CreateOptions{})
if err != nil {
log.Log.Info("Failed to create lease 'kyvernopre-lock'")
}
@ -212,7 +212,7 @@ func main() {
os.Exit(0)
}
le, err := leaderelection.New("kyvernopre", getKyvernoNameSpace(), kubeClientLeaderElection, run, nil, log.Log.WithName("kyvernopre/LeaderElection"))
le, err := leaderelection.New("kyvernopre", config.KyvernoNamespace, kubeClientLeaderElection, run, nil, log.Log.WithName("kyvernopre/LeaderElection"))
if err != nil {
setupLog.Error(err, "failed to elect a leader")
os.Exit(1)
@ -392,7 +392,7 @@ func addPolicyReportSelectorLabel(client *client.Client) {
func removeReportChangeRequest(client *client.Client, kind string) error {
logger := log.Log.WithName("removeReportChangeRequest")
ns := getKyvernoNameSpace()
ns := config.KyvernoNamespace
rcrList, err := client.ListResource("", kind, ns, nil)
if err != nil {
logger.Error(err, "failed to list reportChangeRequest")
@ -434,15 +434,6 @@ func removeViolationCRD(client *client.Client) error {
return nil
}
// getKubePolicyNameSpace - setting default KubePolicyNameSpace
func getKyvernoNameSpace() string {
kyvernoNamespace := os.Getenv("KYVERNO_NAMESPACE")
if kyvernoNamespace == "" {
kyvernoNamespace = "kyverno"
}
return kyvernoNamespace
}
func deleteResource(client *client.Client, apiversion, kind, ns, name string) {
err := client.DeleteResource(apiversion, kind, ns, name, false)
if err != nil && !errors.IsNotFound(err) {

View file

@ -3,9 +3,9 @@ package config
import (
"fmt"
"math"
"os"
"github.com/go-logr/logr"
osutils "github.com/kyverno/kyverno/pkg/utils/os"
rest "k8s.io/client-go/rest"
clientcmd "k8s.io/client-go/tools/clientcmd"
)
@ -65,18 +65,6 @@ const (
// ClusterRoleKind define the default clusterrole resource kind
ClusterRoleKind = "ClusterRole"
)
var (
//KyvernoNamespace is the Kyverno namespace
KyvernoNamespace = getKyvernoNameSpace()
// KyvernoDeploymentName is the Kyverno deployment name
KyvernoDeploymentName = getKyvernoDeploymentName()
//KyvernoServiceName is the Kyverno service name
KyvernoServiceName = getKyvernoServiceName()
//MutatingWebhookServicePath is the path for mutation webhook
MutatingWebhookServicePath = "/mutate"
@ -99,6 +87,15 @@ var (
ReadinessServicePath = "/health/readiness"
)
var (
//KyvernoNamespace is the Kyverno namespace
KyvernoNamespace = osutils.GetEnvWithFallback("KYVERNO_NAMESPACE", "kyverno")
// KyvernoDeploymentName is the Kyverno deployment name
KyvernoDeploymentName = osutils.GetEnvWithFallback("KYVERNO_DEPLOYMENT", "kyverno")
//KyvernoServiceName is the Kyverno service name
KyvernoServiceName = osutils.GetEnvWithFallback("KYVERNO_SVC", "kyverno-svc")
)
//CreateClientConfig creates client config and applies rate limit QPS and burst
func CreateClientConfig(kubeconfig string, qps float64, burst int, log logr.Logger) (*rest.Config, error) {
logger := log.WithName("CreateClientConfig")
@ -126,30 +123,3 @@ func createClientConfig(kubeconfig string, log logr.Logger) (*rest.Config, error
log.V(4).Info("Using specified kubeconfig", "kubeconfig", kubeconfig)
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
// getKubePolicyNameSpace - setting default KubePolicyNameSpace
func getKyvernoNameSpace() string {
kyvernoNamespace := os.Getenv("KYVERNO_NAMESPACE")
if kyvernoNamespace == "" {
kyvernoNamespace = "kyverno"
}
return kyvernoNamespace
}
// getKyvernoServiceName - setting default KyvernoServiceName
func getKyvernoServiceName() string {
webhookServiceName := os.Getenv("KYVERNO_SVC")
if webhookServiceName == "" {
webhookServiceName = "kyverno-svc"
}
return webhookServiceName
}
// getKyvernoDeploymentName - setting default KyvernoServiceName
func getKyvernoDeploymentName() string {
name := os.Getenv("KYVERNO_DEPLOYMENT")
if name == "" {
name = "kyverno"
}
return name
}

View file

@ -73,7 +73,7 @@ func NewMetricsConfigData(rclient kubernetes.Interface, log logr.Logger) (*Metri
}
if cmName != "" {
kyvernoNamespace := getKyvernoNameSpace()
kyvernoNamespace := KyvernoNamespace
configMap, err := rclient.CoreV1().ConfigMaps(kyvernoNamespace).Get(context.TODO(), mcd.cmName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("error occurred while fetching the metrics configmap at %s/%s: %w", kyvernoNamespace, mcd.cmName, err)

10
pkg/utils/os/env.go Normal file
View file

@ -0,0 +1,10 @@
package os
import "os"
func GetEnvWithFallback(name, fallback string) string {
if value := os.Getenv(name); value != "" {
return value
}
return fallback
}