<commit_before>package deployment

import (
	"errors"
	"reflect"
	"time"

	"github.com/golang/glog"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/v1"
	"k8s.io/kubernetes/pkg/runtime"
)

// ManifestStep implements a deployment step
type ManifestStep struct {
	object runtime.Object
}

var _ Step = &ManifestStep{}

// NewManifestStep creates a default step
func NewManifestStep(object runtime.Object) Step {
	return &ManifestStep{
		object: object,
	}
}

func check(name string, r bool) bool {
	if r == false {
		glog.Info("Found difference: " + name)
	}
	return r
}

func compareI(name string, a, b interface{}) bool {
	return check(name, reflect.DeepEqual(a, b))
}

func compareS(name, a, b string) bool {
	return check(name, a == b)
}

func compareContainers(a, b v1.Container) bool {
	return a.Name == b.Name && a.Image == b.Image &&
		compareI("container command", a.Command, b.Command) &&
		compareI("container args", a.Args, b.Args) &&
		compareS("container workdir", a.WorkingDir, b.WorkingDir) &&
		compareI("container ports", a.Ports, b.Ports) &&
		compareI("container env", a.Env, b.Env) &&
		compareI("container resources", a.Resources, b.Resources) &&
		check("container pull policy", a.ImagePullPolicy == b.ImagePullPolicy)
}

func compareContainerLists(a, b []v1.Container) bool {
	aMap := map[string]v1.Container{}
	bMap := map[string]v1.Container{}

	for _, c := range a {
		aMap[c.Name] = c
	}
	for _, c := range b {
		bMap[c.Name] = c
	}

	if !check("conainer list len", len(aMap) == len(bMap)) {
		return false
	}

	for name, ac := range aMap {
		bc, ok := bMap[name]
		if !ok {
			return false
		}
		if !compareContainers(ac, bc) {
			return false
		}
	}
	return true
}

func comparePodSpecs(a, b v1.PodSpec) bool {
	if !check("pod spec containers len", len(a.Containers) == len(b.Containers)) {
		return false
	}
	if !compareContainerLists(a.Containers, b.Containers) {
		return false
	}
	return compareI("pod spec image pull secrets", a.ImagePullSecrets, b.ImagePullSecrets)
}

func comparePods(a, b *v1.Pod) bool {
	return compareI("pod object meta", a.ObjectMeta, b.ObjectMeta) &&
		comparePodSpecs(a.Spec, b.Spec)
}

func compareRCs(a, b *v1.ReplicationController) bool {
	if a.ObjectMeta.Name == "" {
		return false
	}
	return compareI("rc object meta", a.ObjectMeta, b.ObjectMeta) &&
		compareI("rc spec replicas", a.Spec.Replicas, b.Spec.Replicas) &&
		compareI("rc sepc selector", a.Spec.Selector, b.Spec.Selector) &&
		compareI("rc spec template object meta", a.Spec.Template.ObjectMeta, b.Spec.Template.ObjectMeta) &&
		comparePodSpecs(a.Spec.Template.Spec, b.Spec.Template.Spec)
}

// Deploy executes the deployment of a step
func (s *ManifestStep) Deploy() error {
	oGVK := s.object.GetObjectKind().GroupVersionKind()
	switch oGVK.Kind {
	case "ReplicationController":
		var o *v1.ReplicationController
		switch s.object.(type) {
		case *v1.ReplicationController:
			o = s.object.(*v1.ReplicationController)
		case *api.ReplicationController:
			rr := s.object.(*api.ReplicationController)
			if err := scheme.Convert(rr, o); err != nil {
				glog.Error("API object conversion failed.")
				return err
			}
		}
		rc, err := client.ReplicationControllers(namespace).Get(o.ObjectMeta.Name)
		if err == nil && rc != nil {
			if compareRCs(rc, o) {
				glog.Info("Existing RC is identical, skipping deployment")
				return nil<commit_msg>not comparing full object meta objects (#96)<commit_after>package deployment

import (
	"errors"
	"reflect"
	"time"

	"github.com/golang/glog"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/v1"
	"k8s.io/kubernetes/pkg/runtime"
)

// ManifestStep implements a deployment step
type ManifestStep struct {
	object runtime.Object
}

var _ Step = &ManifestStep{}

// NewManifestStep creates a default step
func NewManifestStep(object runtime.Object) Step {
	return &ManifestStep{
		object: object,
	}
}

func check(name string, r bool) bool {
	if r == false {
		glog.Info("Found difference: " + name)
	}
	return r
}

func compareI(name string, a, b interface{}) bool {
	return check(name, reflect.DeepEqual(a, b))
}

func compareS(name, a, b string) bool {
	return check(name, a == b)
}

func compareContainers(a, b v1.Container) bool {
	return a.Name == b.Name && a.Image == b.Image &&
		compareI("container command", a.Command, b.Command) &&
		compareI("container args", a.Args, b.Args) &&
		compareS("container workdir", a.WorkingDir, b.WorkingDir) &&
		compareI("container ports", a.Ports, b.Ports) &&
		compareI("container env", a.Env, b.Env) &&
		compareI("container resources", a.Resources, b.Resources) &&
		check("container pull policy", a.ImagePullPolicy == b.ImagePullPolicy)
}

func compareContainerLists(a, b []v1.Container) bool {
	aMap := map[string]v1.Container{}
	bMap := map[string]v1.Container{}

	for _, c := range a {
		aMap[c.Name] = c
	}
	for _, c := range b {
		bMap[c.Name] = c
	}

	if !check("conainer list len", len(aMap) == len(bMap)) {
		return false
	}

	for name, ac := range aMap {
		bc, ok := bMap[name]
		if !ok {
			return false
		}
		if !compareContainers(ac, bc) {
			return false
		}
	}
	return true
}

func comparePodSpecs(a, b v1.PodSpec) bool {
	if !check("pod spec containers len", len(a.Containers) == len(b.Containers)) {
		return false
	}
	if !compareContainerLists(a.Containers, b.Containers) {
		return false
	}
	return compareI("pod spec image pull secrets", a.ImagePullSecrets, b.ImagePullSecrets)
}

func comparePods(a, b *v1.Pod) bool {
	return compareS("pod object meta name", a.ObjectMeta.Name, b.ObjectMeta.Name) &&
		compareI("pod object meta labels", a.ObjectMeta.Labels, b.ObjectMeta.Labels) &&
		comparePodSpecs(a.Spec, b.Spec)
}

func compareRCs(a, b *v1.ReplicationController) bool {
	if a.ObjectMeta.Name == "" {
		return false
	}
	return compareS("rc object meta name", a.ObjectMeta.Name, b.ObjectMeta.Name) &&
		compareI("rc object meta labels", a.ObjectMeta.Labels, b.ObjectMeta.Labels) &&
		compareI("rc spec replicas", a.Spec.Replicas, b.Spec.Replicas) &&
		compareI("rc sepc selector", a.Spec.Selector, b.Spec.Selector) &&
		compareI("rc spec template object meta labels", a.Spec.Template.ObjectMeta.Labels, b.Spec.Template.ObjectMeta.Labels) &&
		compareS("rc spec template object meta name", a.Spec.Template.ObjectMeta.Name, b.Spec.Template.ObjectMeta.Name) &&
		comparePodSpecs(a.Spec.Template.Spec, b.Spec.Template.Spec)
}

// Deploy executes the deployment of a step
func (s *ManifestStep) Deploy() error {
	oGVK := s.object.GetObjectKind().GroupVersionKind()
	switch oGVK.Kind {
	case "ReplicationController":
		var o *v1.ReplicationController
		switch s.object.(type) {
		case *v1.ReplicationController:
			o = s.object.(*v1.ReplicationController)
		case *api.ReplicationController:
			rr := s.object.(*api.ReplicationController)
			if err := scheme.Convert(rr, o); err != nil {
				glog.Error("API object conversion failed.")
				return err
			}
		}
		rc, err := client.ReplicationControllers(namespace).Get(o.ObjectMeta.Name)
		if err == nil && rc != nil {
			if compareRCs(rc, o) {
				glog.Info("Existing RC is identical, skipping deployment")
				return nil
{'fraction_non_alphanumeric': 0.14343916830570386, 'fraction_numerical': 0.004214667041303737, 'mean_word_length': 4.16243654822335, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6930360', 'n_tokens_mistral': 2652, 'n_tokens_neox': 2497, 'n_words': 616}