-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from joyrex2001/k8s_deployment_support
k8s deployment support
- Loading branch information
Showing
1,392 changed files
with
645 additions
and
643,911 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module github.com/joyrex2001/nightshift | ||
|
||
go 1.13 | ||
|
||
require ( | ||
github.com/Azure/go-autorest/autorest v0.9.4 // indirect | ||
github.com/Azure/go-autorest/autorest/adal v0.8.1 // indirect | ||
github.com/elazarl/go-bindata-assetfs v1.0.0 | ||
github.com/fsnotify/fsnotify v1.4.8-0.20180830220226-ccc981bf8038 // indirect | ||
github.com/gogo/protobuf v1.3.1 // indirect | ||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b | ||
github.com/google/gofuzz v1.1.0 // indirect | ||
github.com/googleapis/gnostic v0.4.0 // indirect | ||
github.com/hashicorp/hcl v1.0.1-0.20180906183839-65a6292f0157 // indirect | ||
github.com/imdario/mergo v0.3.8 // indirect | ||
github.com/julienschmidt/httprouter v1.3.0 | ||
github.com/kr/pretty v0.1.0 | ||
github.com/openshift/api v3.9.0+incompatible | ||
github.com/openshift/client-go v0.0.0-20200116152001-92a2713fa240 | ||
github.com/pelletier/go-toml v1.6.0 // indirect | ||
github.com/prometheus/client_golang v1.4.0 | ||
github.com/prometheus/client_model v0.2.0 | ||
github.com/spf13/cast v1.3.1 // indirect | ||
github.com/spf13/cobra v0.0.5 | ||
github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 | ||
github.com/spf13/viper v1.6.2 | ||
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect | ||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect | ||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect | ||
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 // indirect | ||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect | ||
google.golang.org/appengine v1.6.5 // indirect | ||
gopkg.in/ini.v1 v1.51.1 // indirect | ||
gopkg.in/yaml.v2 v2.2.8 | ||
k8s.io/api v0.17.2 | ||
k8s.io/apimachinery v0.17.2 | ||
k8s.io/client-go v0.17.1 | ||
k8s.io/utils v0.0.0-20200124190032-861946025e34 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package scanner | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang/glog" | ||
v1 "k8s.io/api/apps/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
|
||
_ "k8s.io/client-go/plugin/pkg/client/auth/azure" | ||
) | ||
|
||
// DeploymentScanner is the object that implements scanning of kubernetes | ||
// Deployments. | ||
type DeploymentScanner struct { | ||
config Config | ||
kubernetes *rest.Config | ||
} | ||
|
||
func init() { | ||
RegisterModule("deployment", NewDeploymentScanner) | ||
} | ||
|
||
// NewDeploymentScanner will instantiate a new DeploymentScanner object. | ||
func NewDeploymentScanner() (Scanner, error) { | ||
kubernetes, err := getKubernetes() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed instantiating k8s client: %s", err) | ||
} | ||
return &DeploymentScanner{ | ||
kubernetes: kubernetes, | ||
}, nil | ||
} | ||
|
||
// SetConfig will set the generic configuration for this scanner. | ||
func (s *DeploymentScanner) SetConfig(cfg Config) { | ||
s.config = cfg | ||
} | ||
|
||
// GetConfig will return the config applied for this scanner. | ||
func (s *DeploymentScanner) GetConfig() Config { | ||
return s.config | ||
} | ||
|
||
// GetObjects will return a populated list of Objects containing the relavant | ||
// resources with their schedule info. | ||
func (s *DeploymentScanner) GetObjects() ([]*Object, error) { | ||
rcs, err := s.getDeployments() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s.getObjects(rcs) | ||
} | ||
|
||
// Scale will scale a given object to given amount of replicas. | ||
func (s *DeploymentScanner) Scale(obj *Object, replicas int) error { | ||
glog.Infof("Scaling %s/%s to %d replicas", obj.Namespace, obj.Name, replicas) | ||
apps, err := kubernetes.NewForConfig(s.kubernetes) | ||
if err != nil { | ||
return err | ||
} | ||
scale, err := apps.AppsV1().Deployments(obj.Namespace).GetScale(obj.Name, metav1.GetOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("GetScale failed with: %s", err) | ||
} | ||
scale.Spec.Replicas = int32(replicas) | ||
_, err = apps.AppsV1().Deployments(obj.Namespace).UpdateScale(obj.Name, scale) | ||
return err | ||
} | ||
|
||
// SaveState will save the current number of replicas as an annotation on the | ||
// deployment config. | ||
func (s *DeploymentScanner) SaveState(obj *Object) (int, error) { | ||
dc, err := s.getDeployment(obj) | ||
if err != nil { | ||
return 0, err | ||
} | ||
repl := int(*dc.Spec.Replicas) | ||
dc.ObjectMeta = updateState(dc.ObjectMeta, repl) | ||
apps, _ := kubernetes.NewForConfig(s.kubernetes) | ||
_, err = apps.AppsV1().Deployments(obj.Namespace).Update(dc) | ||
return repl, err | ||
} | ||
|
||
// getDeployment will return an Deployment object. | ||
func (s *DeploymentScanner) getDeployment(obj *Object) (*v1.Deployment, error) { | ||
apps, err := kubernetes.NewForConfig(s.kubernetes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return apps.AppsV1().Deployments(obj.Namespace).Get(obj.Name, metav1.GetOptions{}) | ||
} | ||
|
||
// getDeployments will return all deploymentconfigs in the namespace that | ||
// match the label selector. | ||
func (s *DeploymentScanner) getDeployments() (*v1.DeploymentList, error) { | ||
apps, err := kubernetes.NewForConfig(s.kubernetes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return apps.AppsV1().Deployments(s.config.Namespace).List(metav1.ListOptions{ | ||
LabelSelector: s.config.Label, | ||
}) | ||
} | ||
|
||
// getObjects will itterate through the list of deployment configs and populate | ||
// a list of objects containing the schedule configuration (if any). | ||
func (s *DeploymentScanner) getObjects(rcs *v1.DeploymentList) ([]*Object, error) { | ||
objs := []*Object{} | ||
for _, rc := range rcs.Items { | ||
obj, err := s.unmarshall(&rc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if obj.Schedule != nil { | ||
objs = append(objs, obj) | ||
} | ||
} | ||
return objs, nil | ||
} | ||
|
||
// Watch will return a channel on which Event objects will be published that | ||
// describe change events in the cluster. | ||
func (s *DeploymentScanner) Watch(_stop chan bool) (chan Event, error) { | ||
return watcher(_stop, s.getWatcher, s.unmarshall) | ||
} | ||
|
||
// getWatcher will return a watcher for Deployments | ||
func (s *DeploymentScanner) getWatcher() (watch.Interface, error) { | ||
apps, err := kubernetes.NewForConfig(s.kubernetes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return apps.AppsV1().Deployments(s.config.Namespace).Watch(metav1.ListOptions{ | ||
LabelSelector: s.config.Label, | ||
}) | ||
} | ||
|
||
// getObject will convert a deploymentconfig object to a scanner.Object. | ||
func (s *DeploymentScanner) unmarshall(kobj interface{}) (*Object, error) { | ||
m, ok := kobj.(*v1.Deployment) | ||
if !ok { | ||
return nil, fmt.Errorf("can't unmarshall %v to Deployment", m) | ||
} | ||
obj := NewObjectForScanner(s) | ||
if err := obj.updateWithMeta(m.ObjectMeta); err != nil { | ||
glog.Error(err) | ||
} | ||
obj.Replicas = int(*m.Spec.Replicas) | ||
return obj, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.