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

Update registry creds #109

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
registry-creds
.vscode/
vendor
.vscode/
### Example user template template
### Example user template

# IntelliJ project files
.idea
*.iml
out
gen
19 changes: 14 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
FROM alpine:3.4
MAINTAINER Steve Sloka <steve@stevesloka.com>
# Start by building the application.
FROM golang:1.19 as build

RUN apk add --update ca-certificates && \
rm -rf /var/cache/apk/*
WORKDIR /go/src/app

COPY registry-creds registry-creds
ADD . /go/src/app/registry-creds

WORKDIR /go/src/app/registry-creds

RUN go get -d -v ./...
RUN go test
RUN GOOS=linux GOARCH=amd64 go build -v -ldflags="-w -s" -o /go/bin/app/registry-creds

# Now copy it into our base image.
FROM gcr.io/distroless/base

COPY --from=build /go/bin/app/registry-creds /
ENTRYPOINT ["/registry-creds"]
60 changes: 60 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module github.com/achoimet/registry-creds

go 1.18

require (
github.com/aws/aws-sdk-go v1.44.75
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/sirupsen/logrus v1.9.0
github.com/spf13/pflag v1.0.5
golang.org/x/net v0.0.0-20220811182439-13a9a731de15
golang.org/x/oauth2 v0.0.0-20220808172628-8227340efae7
k8s.io/api v0.24.3
k8s.io/apimachinery v0.24.3
k8s.io/client-go v0.24.3
)

require (
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)

require (
cloud.google.com/go/compute v1.8.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.22.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/stretchr/testify v1.8.0
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.70.1 // indirect
k8s.io/kube-openapi v0.0.0-20220803164354-a70c9af30aea // indirect
k8s.io/utils v0.0.0-20220810061631-2e139fc3ae1e // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
693 changes: 693 additions & 0 deletions go.sum

Large diffs are not rendered by default.

43 changes: 0 additions & 43 deletions k8s/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,3 @@ spec:
secretKeyRef:
name: registry-creds-ecr
key: aws-region
- name: aws_assume_role
valueFrom:
secretKeyRef:
name: registry-creds-ecr
key: aws-assume-role
- name: DOCKER_PRIVATE_REGISTRY_PASSWORD
valueFrom:
secretKeyRef:
name: registry-creds-dpr
key: DOCKER_PRIVATE_REGISTRY_PASSWORD
- name: DOCKER_PRIVATE_REGISTRY_SERVER
valueFrom:
secretKeyRef:
name: registry-creds-dpr
key: DOCKER_PRIVATE_REGISTRY_SERVER
- name: DOCKER_PRIVATE_REGISTRY_USER
valueFrom:
secretKeyRef:
name: registry-creds-dpr
key: DOCKER_PRIVATE_REGISTRY_USER
- name: ACR_URL
valueFrom:
secretKeyRef:
name: registry-creds-acr
key: ACR_URL
- name: ACR_CLIENT_ID
valueFrom:
secretKeyRef:
name: registry-creds-acr
key: ACR_CLIENT_ID
- name: ACR_PASSWORD
valueFrom:
secretKeyRef:
name: registry-creds-acr
key: ACR_PASSWORD
volumeMounts:
- name: gcr-creds
mountPath: "/root/.config/gcloud"
readOnly: true
volumes:
- name: gcr-creds
secret:
secretName: registry-creds-gcr
81 changes: 41 additions & 40 deletions k8sutil/k8sutil.go → k8sutil.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package k8sutil
package main

import (
"context"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"log"
"time"

"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
core_v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
coreType "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/fields"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -19,12 +21,12 @@ type KubeInterface interface {
Secrets(namespace string) coreType.SecretInterface
Namespaces() coreType.NamespaceInterface
ServiceAccounts(namespace string) coreType.ServiceAccountInterface
Core() coreType.CoreV1Interface
}

type K8sutilInterface struct {
Kclient KubeInterface
MasterHost string
KubernetesClient kubernetes.Interface
KubernetesCoreV1 KubeInterface
MasterHost string
}

// New creates a new instance of k8sutil
Expand All @@ -37,14 +39,15 @@ func New(kubeCfgFile, masterHost string) (*K8sutilInterface, error) {
}

k := &K8sutilInterface{
Kclient: client,
MasterHost: masterHost,
KubernetesClient: client,
KubernetesCoreV1: client.CoreV1().(KubeInterface),
MasterHost: masterHost,
}

return k, nil
}

func newKubeClient(kubeCfgFile string) (KubeInterface, error) {
func newKubeClient(kubeCfgFile string) (*kubernetes.Clientset, error) {

var client *kubernetes.Clientset

Expand Down Expand Up @@ -82,8 +85,8 @@ func newKubeClient(kubeCfgFile string) (KubeInterface, error) {
}

// GetNamespaces returns all namespaces
func (k *K8sutilInterface) GetNamespaces() (*v1.NamespaceList, error) {
namespaces, err := k.Kclient.Namespaces().List(v1.ListOptions{})
func (k *K8sutilInterface) GetNamespaces() (*core_v1.NamespaceList, error) {
namespaces, err := k.KubernetesCoreV1.Namespaces().List(context.TODO(), v12.ListOptions{})
if err != nil {
logrus.Error("Error getting namespaces: ", err)
return nil, err
Expand All @@ -93,8 +96,8 @@ func (k *K8sutilInterface) GetNamespaces() (*v1.NamespaceList, error) {
}

// GetSecret get a secret
func (k *K8sutilInterface) GetSecret(namespace, secretname string) (*v1.Secret, error) {
secret, err := k.Kclient.Secrets(namespace).Get(secretname)
func (k *K8sutilInterface) GetSecret(namespace, secretname string) (*core_v1.Secret, error) {
secret, err := k.KubernetesCoreV1.Secrets(namespace).Get(context.TODO(), secretname, v12.GetOptions{})
if err != nil {
logrus.Error("Error getting secret: ", err)
return nil, err
Expand All @@ -104,8 +107,8 @@ func (k *K8sutilInterface) GetSecret(namespace, secretname string) (*v1.Secret,
}

// CreateSecret creates a secret
func (k *K8sutilInterface) CreateSecret(namespace string, secret *v1.Secret) error {
_, err := k.Kclient.Secrets(namespace).Create(secret)
func (k *K8sutilInterface) CreateSecret(namespace string, secret *core_v1.Secret) error {
_, err := k.KubernetesCoreV1.Secrets(namespace).Create(context.TODO(), secret, v12.CreateOptions{})

if err != nil {
logrus.Error("Error creating secret: ", err)
Expand All @@ -116,8 +119,8 @@ func (k *K8sutilInterface) CreateSecret(namespace string, secret *v1.Secret) err
}

// UpdateSecret updates a secret
func (k *K8sutilInterface) UpdateSecret(namespace string, secret *v1.Secret) error {
_, err := k.Kclient.Secrets(namespace).Update(secret)
func (k *K8sutilInterface) UpdateSecret(namespace string, secret *core_v1.Secret) error {
_, err := k.KubernetesCoreV1.Secrets(namespace).Update(context.TODO(), secret, v12.UpdateOptions{})

if err != nil {
logrus.Error("Error updating secret: ", err)
Expand All @@ -128,8 +131,8 @@ func (k *K8sutilInterface) UpdateSecret(namespace string, secret *v1.Secret) err
}

// GetServiceAccount updates a secret
func (k *K8sutilInterface) GetServiceAccount(namespace, name string) (*v1.ServiceAccount, error) {
sa, err := k.Kclient.ServiceAccounts(namespace).Get(name)
func (k *K8sutilInterface) GetServiceAccount(namespace, name string) (*core_v1.ServiceAccount, error) {
sa, err := k.KubernetesCoreV1.ServiceAccounts(namespace).Get(context.TODO(), name, v12.GetOptions{})

if err != nil {
logrus.Error("Error getting service account: ", err)
Expand All @@ -140,8 +143,8 @@ func (k *K8sutilInterface) GetServiceAccount(namespace, name string) (*v1.Servic
}

// UpdateServiceAccount updates a secret
func (k *K8sutilInterface) UpdateServiceAccount(namespace string, sa *v1.ServiceAccount) error {
_, err := k.Kclient.ServiceAccounts(namespace).Update(sa)
func (k *K8sutilInterface) UpdateServiceAccount(namespace string, sa *core_v1.ServiceAccount) error {
_, err := k.KubernetesCoreV1.ServiceAccounts(namespace).Update(context.TODO(), sa, v12.UpdateOptions{})

if err != nil {
logrus.Error("Error updating service account: ", err)
Expand All @@ -151,24 +154,22 @@ func (k *K8sutilInterface) UpdateServiceAccount(namespace string, sa *v1.Service
return nil
}

func (k *K8sutilInterface) WatchNamespaces(resyncPeriod time.Duration, handler func(*v1.Namespace) error) {
func (k *K8sutilInterface) WatchNamespaces(resyncPeriod time.Duration, handler func(*core_v1.Namespace) error) {
factory := informers.NewSharedInformerFactory(k.KubernetesClient, resyncPeriod)
informer := factory.Core().V1().Namespaces().Informer()
stopC := make(chan struct{})
_, c := cache.NewInformer(
cache.NewListWatchFromClient(k.Kclient.Core().RESTClient(), "namespaces", v1.NamespaceAll, fields.Everything()),
&v1.Namespace{},
resyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if err := handler(obj.(*v1.Namespace)); err != nil {
log.Println(err)
}
},
UpdateFunc: func(_ interface{}, obj interface{}) {
if err := handler(obj.(*v1.Namespace)); err != nil {
log.Println(err)
}
},
defer close(stopC)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if err := handler(obj.(*core_v1.Namespace)); err != nil {
log.Println(err)
}
},
)
c.Run(stopC)
UpdateFunc: func(_ interface{}, obj interface{}) {
if err := handler(obj.(*core_v1.Namespace)); err != nil {
log.Println(err)
}
},
})
informer.Run(stopC)
}
Loading