Skip to content

Commit

Permalink
Merge branch 'master' into fix-gcr
Browse files Browse the repository at this point in the history
  • Loading branch information
stevesloka authored Feb 24, 2017
2 parents 6836de8 + 265fa8b commit 5e06d5b
Show file tree
Hide file tree
Showing 6,802 changed files with 3,288,911 additions and 454,367 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: go

go:
- 1.8

sudo: required

services:
- docker

script:
- make test
- make build
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016, UPMC Enterprises
Copyright (c) 2017, UPMC Enterprises
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

.PHONY: all binary container push clean test

TAG = 1.5
TAG = 1.6
PREFIX = upmcenterprises

all: container

binary: main.go
build: main.go
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -installsuffix cgo -o registry-creds --ldflags '-w' ./main.go

container: binary
container: build
docker build -t $(PREFIX)/registry-creds:$(TAG) .

push:
Expand All @@ -22,4 +22,4 @@ clean:
rm -f registry-creds

test: clean
go test $(go list ./... | grep -v /vendor/)
go test $(go list ./... | grep -v vendor)
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ kubectl create -f k8s/secret.yaml
3. Create the replication controller. NOTE: If running on prem, no need to provide AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY since that will come from the EC2 instance.

```bash
kubectl create -f k8s/replicationController.yml
kubectl create -f k8s/replicationController.yaml
```
4. Use awsecr-cred for name of imagePullSecrets on your deployment.yaml file.

## How to setup running in GCR

Expand All @@ -76,7 +77,7 @@ kubectl create -f k8s/secret.yml
3. Create the replication controller:

```bash
kubectl create -f k8s/replicationController.yml
kubectl create -f k8s/replicationController.yaml
```

## DockerHub Image
Expand Down
16 changes: 10 additions & 6 deletions k8s/replicationController.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ metadata:
name: registry-creds
namespace: kube-system
labels:
version: v1.5
version: v1.6
spec:
replicas: 1
selector:
name: registry-creds
version: v1.5
version: v1.6
template:
metadata:
labels:
name: registry-creds
version: v1.5
version: v1.6
spec:
containers:
- image: upmcenterprises/registry-creds:1.5
- image: upmcenterprises/registry-creds:1.6
name: registry-creds
imagePullPolicy: Always
env:
Expand All @@ -36,12 +36,16 @@ spec:
secretKeyRef:
name: registry-creds
key: aws-account
- name: awsregion
valueFrom:
secretKeyRef:
name: registry-creds
key: aws-region
volumeMounts:
- name: gcr-creds
mountPath: "/root/.config/gcloud"
readOnly: true
volumes:
- name: gcr-creds
secret:
secretName: gcr-secret

secretName: gcr-secret
146 changes: 146 additions & 0 deletions k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package k8sutil

import (
"github.com/Sirupsen/logrus"
"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/rest"
"k8s.io/client-go/tools/clientcmd"
)

// KubeInterface abstracts the k8s api
type KubeInterface interface {
Secrets(namespace string) coreType.SecretInterface
Namespaces() coreType.NamespaceInterface
ServiceAccounts(namespace string) coreType.ServiceAccountInterface
}

type K8sutilInterface struct {
Kclient KubeInterface
MasterHost string
}

// New creates a new instance of k8sutil
func New(kubeCfgFile, masterHost string) (*K8sutilInterface, error) {

client, err := newKubeClient(kubeCfgFile)

if err != nil {
logrus.Fatalf("Could not init Kubernetes client! [%s]", err)
}

k := &K8sutilInterface{
Kclient: client,
MasterHost: masterHost,
}

return k, nil
}

func newKubeClient(kubeCfgFile string) (KubeInterface, error) {

var client *kubernetes.Clientset

// Should we use in cluster or out of cluster config
if len(kubeCfgFile) == 0 {
logrus.Info("Using InCluster k8s config")
cfg, err := rest.InClusterConfig()

if err != nil {
return nil, err
}

client, err = kubernetes.NewForConfig(cfg)

if err != nil {
return nil, err
}
} else {
logrus.Infof("Using OutOfCluster k8s config with kubeConfigFile: %s", kubeCfgFile)
cfg, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile)

if err != nil {
logrus.Error("Got error trying to create client: ", err)
return nil, err
}

client, err = kubernetes.NewForConfig(cfg)

if err != nil {
return nil, err
}
}

return client, nil
}

// GetNamespaces returns all namespaces
func (k *K8sutilInterface) GetNamespaces() (*v1.NamespaceList, error) {
namespaces, err := k.Kclient.Namespaces().List(v1.ListOptions{})
if err != nil {
logrus.Error("Error getting namespaces: ", err)
return nil, err
}

return namespaces, nil
}

// GetSecret get a secret
func (k *K8sutilInterface) GetSecret(namespace, secretname string) (*v1.Secret, error) {
secret, err := k.Kclient.Secrets(namespace).Get(secretname)
if err != nil {
logrus.Error("Error getting secret: ", err)
return nil, err
}

return secret, nil
}

// CreateSecret creates a secret
func (k *K8sutilInterface) CreateSecret(namespace string, secret *v1.Secret) error {
_, err := k.Kclient.Secrets(namespace).Create(secret)

if err != nil {
logrus.Error("Error creating secret: ", err)
return err
}

return nil
}

// UpdateSecret updates a secret
func (k *K8sutilInterface) UpdateSecret(namespace string, secret *v1.Secret) error {
_, err := k.Kclient.Secrets(namespace).Update(secret)

if err != nil {
logrus.Error("Error updating secret: ", err)
return err
}

return nil
}

// GetServiceAccount updates a secret
func (k *K8sutilInterface) GetServiceAccount(namespace, name string) (*v1.ServiceAccount, error) {
sa, err := k.Kclient.ServiceAccounts(namespace).Get(name)

if err != nil {
logrus.Error("Error getting service account: ", err)
return nil, err
}

return sa, nil
}

// UpdateServiceAccount updates a secret
func (k *K8sutilInterface) UpdateServiceAccount(namespace string, sa *v1.ServiceAccount) error {
_, err := k.Kclient.ServiceAccounts(namespace).Update(sa)

if err != nil {
logrus.Error("Error updating service account: ", err)
return err
}

return nil
}
Loading

0 comments on commit 5e06d5b

Please sign in to comment.