Skip to content

Commit

Permalink
Fix namespace bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hooksie1 committed Mar 5, 2022
1 parent 54fe69d commit 275f2cd
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
1 change: 0 additions & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func generate(cmd *cobra.Command, args []string) {
mSecret := fmt.Sprintf("mutating-%s", viper.GetString("secret"))
vSecret := fmt.Sprintf("validating-%s", viper.GetString("secret"))
port := viper.GetInt("port")
namespace := viper.GetString("namespace")

mCert, mKey, err := deployment.GenerateCertificate(mService, namespace)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func mutateServer(cmd *cobra.Command, args []string) {
}

injector := server.SidecarInjector{
Client: mgr.GetClient(),
Client: mgr.GetClient(),
Namespace: namespace,
}
log.Info("setting up server")
mgrServer := mgr.GetWebhookServer()
Expand Down
2 changes: 0 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ func init() {
viper.BindPFlag("service", serverCmd.PersistentFlags().Lookup("service"))
serverCmd.PersistentFlags().String("secret", "cmsnr-secret", "Secret name")
viper.BindPFlag("secret", serverCmd.PersistentFlags().Lookup("secret"))
serverCmd.PersistentFlags().String("namespace", "default", "Namespace")
viper.BindPFlag("namespace", serverCmd.PersistentFlags().Lookup("namespace"))

}

Expand Down
6 changes: 4 additions & 2 deletions pkg/deployment/deployment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package deployment

import (
"fmt"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -59,10 +61,10 @@ func (d *Deployment) getTemplate() corev1.PodTemplateSpec {
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Image: "hooksie1/cmsnr",
Image: "hooksie1/cmsnr:latest",
ImagePullPolicy: "Always",
Name: d.Name,
Args: []string{"server", "start", d.ServerType},
Args: []string{"server", "start", d.ServerType, fmt.Sprintf("-n=%s", d.Namespace)},
Ports: []corev1.ContainerPort{
{
Name: "https",
Expand Down
11 changes: 6 additions & 5 deletions pkg/server/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ type Config struct {
}

type SidecarInjector struct {
Client client.Client
decoder *admission.Decoder
Namespace string
Client client.Client
decoder *admission.Decoder
}

func getContainers(depName string) []corev1.Container {
func getContainers(namespace, depName string) []corev1.Container {
return []corev1.Container{
{
Name: "opa",
Expand All @@ -33,7 +34,7 @@ func getContainers(depName string) []corev1.Container {
Name: "cmsnr-client",
Image: "hooksie1/cmsnr:latest",
ImagePullPolicy: corev1.PullPolicy("IfNotPresent"),
Args: []string{"opa", "watch", fmt.Sprintf("-d=%s", depName)},
Args: []string{"opa", "watch", fmt.Sprintf("-d=%s", depName), fmt.Sprintf("-n=%s", namespace)},
},
}
}
Expand Down Expand Up @@ -61,7 +62,7 @@ func (s *SidecarInjector) Handle(ctx context.Context, r admission.Request) admis

if checkInject(pod) {
log.Infof("Injecting sidecar for %s", pod.Name)
pod.Spec.Containers = append(pod.Spec.Containers, getContainers(pod.Annotations["cmsnr.com/deploymentName"])...)
pod.Spec.Containers = append(pod.Spec.Containers, getContainers(s.Namespace, pod.Annotations["cmsnr.com/deploymentName"])...)
if pod.Spec.ServiceAccountName == "default" {
log.Info("no service account defined, adding cmsnr account")
pod.Spec.ServiceAccountName = "cmsnr"
Expand Down

0 comments on commit 275f2cd

Please sign in to comment.