<commit_before>// Copyright (c) 2016, <NAME>.
// This source file is part of the KUBE-AMQP-AUTOSCALE open source project
// Licensed under Apache License v2.0
// See LICENSE file for license information

package main

import (
	"fmt"
	"io/ioutil"
	"log"

	"k8s.io/kubernetes/pkg/client/restclient"
	client "k8s.io/kubernetes/pkg/client/unversioned"
	"k8s.io/kubernetes/pkg/util/crypto"
)

const (
	deploymentKind            = "Deployment"
	jobKind                   = "Job"
	replicationControllerKind = "ReplicationController"
	replicaSetKind            = "ReplicaSet"
)

type apiContext struct {
	URL       string
	User      string
	Passwd    <PASSWORD>
	TokenFile string
	CAFile    string
	Insecure  bool
	Bounds    *scaleBounds

	clientConf *restclient.Config
}

func scale(kind string, ns string, name string, newSize int32, ctx *apiContext) error {
	c, err := ctx.client()
	if err != nil {
		return err
	}
	return scaleKind(c, kind, ns, name, newSize, ctx.Bounds)
}

func scaleKind(c *client.Client, kind string, ns string, name string, newSize int32, b *scaleBounds) error {
	switch kind {
	case replicationControllerKind:
		return scaleReplicationControllers(c, ns, name, newSize, b)
	case replicaSetKind:
		return scaleReplicaSets(c, ns, name, newSize, b)
	case deploymentKind:
		return scaleDeployments(c, ns, name, newSize, b)
	}
	return fmt.Errorf("No scaler has been implemented for '%s'", kind)
}

func scaleDeployments(c *client.Client, ns string, name string, newSize int32, b *scaleBounds) error {
	pod, err := c.Deployments(ns).Get(name)
	if err != nil {
		return err
	}
	replicas := b.newSize(pod.Spec.Replicas, newSize)
	if replicas != pod.Spec.Replicas {
		log.Printf("Scaling deployment '%s' from %d to %d replicas", name, pod.Spec.Replicas, replicas)
		_, err = c.Deployments(ns).Update(pod)
		if err != nil {
			return err
		}
	}
	return nil
}

func scaleReplicationControllers(c *client.Client, ns string, name string, newSize int32, b *scaleBounds) error {
	pod, err := c.ReplicationControllers(ns).Get(name)
	if err != nil {
		return err
	}
	replicas := b.newSize(pod.Spec.Replicas, newSize)
	if replicas != pod.Spec.Replicas {
		log.Printf("Scaling replication controller '%s' from %d to %d replicas", name, pod.Spec.Replicas, replicas)
		_, err = c.ReplicationControllers(ns).Update(pod)
		if err != nil {
			return err
		}
	}
	return nil
}

func scaleReplicaSets(c *client.Client, ns string, name string, newSize int32, b *scaleBounds) error {
	pod, err := c.ReplicaSets(ns).Get(name)
	if err != nil {
		return err
	}
	replicas := b.newSize(pod.Spec.Replicas, newSize)
	if replicas != pod.Spec.Replicas {
		log.Printf("Scaling replica set '%s' from %d to %d replicas", name, pod.Spec.Replicas, replicas)
		_, err = c.ReplicaSets(ns).Update(pod)
		if err != nil {
			return err
		}
	}
	return nil
}

func (ctx *apiContext) client() (*client.Client, error) {
	if ctx.clientConf == nil {
		conf, err := apiConfig(ctx.URL, ctx.User, ctx.Passwd, ctx.TokenFile, ctx.CAFile, ctx.Insecure)
		if err != nil {
			return nil, err<commit_msg>Set new value for replicas<commit_after>// Copyright (c) 2016, <NAME>.
// This source file is part of the KUBE-AMQP-AUTOSCALE open source project
// Licensed under Apache License v2.0
// See LICENSE file for license information

package main

import (
	"fmt"
	"io/ioutil"
	"log"

	"k8s.io/kubernetes/pkg/client/restclient"
	client "k8s.io/kubernetes/pkg/client/unversioned"
	"k8s.io/kubernetes/pkg/util/crypto"
)

const (
	deploymentKind            = "Deployment"
	jobKind                   = "Job"
	replicationControllerKind = "ReplicationController"
	replicaSetKind            = "ReplicaSet"
)

type apiContext struct {
	URL       string
	User      string
	Passwd    string
	TokenFile string
	CAFile    string
	Insecure  bool
	Bounds    *scaleBounds

	clientConf *restclient.Config
}

func scale(kind string, ns string, name string, newSize int32, ctx *apiContext) error {
	c, err := ctx.client()
	if err != nil {
		return err
	}
	return scaleKind(c, kind, ns, name, newSize, ctx.Bounds)
}

func scaleKind(c *client.Client, kind string, ns string, name string, newSize int32, b *scaleBounds) error {
	switch kind {
	case replicationControllerKind:
		return scaleReplicationControllers(c, ns, name, newSize, b)
	case replicaSetKind:
		return scaleReplicaSets(c, ns, name, newSize, b)
	case deploymentKind:
		return scaleDeployments(c, ns, name, newSize, b)
	}
	return fmt.Errorf("No scaler has been implemented for '%s'", kind)
}

func scaleDeployments(c *client.Client, ns string, name string, newSize int32, b *scaleBounds) error {
	pod, err := c.Deployments(ns).Get(name)
	if err != nil {
		return err
	}
	replicas := b.newSize(pod.Spec.Replicas, newSize)
	if replicas != pod.Spec.Replicas {
		log.Printf("Scaling deployment '%s' from %d to %d replicas", name, pod.Spec.Replicas, replicas)
		pod.Spec.Replicas = replicas
		_, err = c.Deployments(ns).Update(pod)
		if err != nil {
			return err
		}
	}
	return nil
}

func scaleReplicationControllers(c *client.Client, ns string, name string, newSize int32, b *scaleBounds) error {
	pod, err := c.ReplicationControllers(ns).Get(name)
	if err != nil {
		return err
	}
	replicas := b.newSize(pod.Spec.Replicas, newSize)
	if replicas != pod.Spec.Replicas {
		log.Printf("Scaling replication controller '%s' from %d to %d replicas", name, pod.Spec.Replicas, replicas)
		pod.Spec.Replicas = replicas
		_, err = c.ReplicationControllers(ns).Update(pod)
		if err != nil {
			return err
		}
	}
	return nil
}

func scaleReplicaSets(c *client.Client, ns string, name string, newSize int32, b *scaleBounds) error {
	pod, err := c.ReplicaSets(ns).Get(name)
	if err != nil {
		return err
	}
	replicas := b.newSize(pod.Spec.Replicas, newSize)
	if replicas != pod.Spec.Replicas {
		log.Printf("Scaling replica set '%s' from %d to %d replicas", name, pod.Spec.Replicas, replicas)
		pod.Spec.Replicas = replicas
		_, err = c.ReplicaSets(ns).Update(pod)
		if err != nil {
			return err
		}
	}
	return nil
}

func (ctx *apiContext) client() (*client.Client, error) {
	if ctx.clientConf == nil {
		conf, err := apiConfig(ctx.URL, ctx.User, ctx.Passwd, ctx.TokenFile, ctx.CAFile, ctx.Insecure)
		if err != nil {
			return nil, err
{'fraction_non_alphanumeric': 0.1207395498392283, 'fraction_numerical': 0.006109324758842444, 'mean_word_length': 3.8075734157650696, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '541615', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2214, 'n_words': 617}