<commit_before>package install

import (
	"fmt"

	"github.com/coreos-inc/operator-client/pkg/client"
	"k8s.io/api/core/v1"
	"k8s.io/api/extensions/v1beta1"
	rbac "k8s.io/api/rbac/v1beta1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	"github.com/coreos-inc/alm/apis"
	"github.com/coreos-inc/alm/apis/clusterserviceversion/v1alpha1"
)

const (
	InstallStrategyNameDeployment = "deployment"
)

var BlockOwnerDeletion = true
var Controller = false

// StrategyDeploymentPermissions describe the rbac rules and service account needed by the install strategy
type StrategyDeploymentPermissions struct {
	ServiceAccountName string            `json:"serviceAccountName"`
	Rules              []rbac.PolicyRule `json:"rules"`
}

// StrategyDetailsDeployment represents the parsed details of a Deployment
// InstallStrategy.
type StrategyDetailsDeployment struct {
	DeploymentSpecs []v1beta1.DeploymentSpec        `json:"deployments"`
	Permissions     []StrategyDeploymentPermissions `json:"permissions"`
}

func (d *StrategyDetailsDeployment) Install(
	client client.Interface,
	owner metav1.ObjectMeta,
	ownerType metav1.TypeMeta,
) error {
	for _, permission := range d.Permissions {
		// create role
		role := &rbac.Role{
			Rules: permission.Rules,
		}
		role.SetGenerateName(fmt.Sprintf("%s-role-", owner.Name))
		createdRole, err := client.KubernetesInterface().RbacV1beta1().Roles(owner.Namespace).Create(role)
		if err != nil {
			return err
		}

		// create serviceaccount
		serviceAccount := &v1.ServiceAccount{}
		serviceAccount.SetName(permission.ServiceAccountName)
		serviceAccount, err = client.KubernetesInterface().CoreV1().ServiceAccounts(owner.Namespace).Create(serviceAccount)
		if err != nil {
			return err
		}

		// create rolebinding
		roleBinding := &rbac.RoleBinding{
			RoleRef: rbac.RoleRef{
				Kind:     "Role",
				Name:     createdRole.GetName(),
				APIGroup: rbac.GroupName},
			Subjects: []rbac.Subject{{
				Kind:      "ServiceAccount",
				Name:      serviceAccount.Name,
				Namespace: serviceAccount.Namespace,
			}},
		}
		roleBinding.SetGenerateName(fmt.Sprintf("%s-%s-rolebinding-", createdRole.Name, serviceAccount.Name))

		if _, err = client.KubernetesInterface().RbacV1beta1().RoleBindings(owner.Namespace).Create(roleBinding); err != nil {
			return err
		}
	}

	for _, spec := range d.DeploymentSpecs {
		ownerReferences := []metav1.OwnerReference{
			{
				APIVersion:         apis.GroupName,
				Kind:               v1alpha1.ClusterServiceVersionKind,
				Name:               owner.GetName(),
				UID:                owner.UID,
				Controller:         &Controller,
				BlockOwnerDeletion: &BlockOwnerDeletion,
			},
		}
		dep := v1beta1.Deployment{Spec: spec}
		dep.SetNamespace(owner.Namespace)
		dep.SetOwnerReferences(ownerReferences)
		dep.SetGenerateName(fmt.Sprintf("%s-", owner.Name))<commit_msg>Dob't create serviceaccount if it already exists<commit_after>package install

import (
	"fmt"

	"github.com/coreos-inc/operator-client/pkg/client"
	"k8s.io/api/core/v1"
	"k8s.io/api/extensions/v1beta1"
	rbac "k8s.io/api/rbac/v1beta1"
	"k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	"github.com/coreos-inc/alm/apis"
	"github.com/coreos-inc/alm/apis/clusterserviceversion/v1alpha1"
)

const (
	InstallStrategyNameDeployment = "deployment"
)

var BlockOwnerDeletion = true
var Controller = false

// StrategyDeploymentPermissions describe the rbac rules and service account needed by the install strategy
type StrategyDeploymentPermissions struct {
	ServiceAccountName string            `json:"serviceAccountName"`
	Rules              []rbac.PolicyRule `json:"rules"`
}

// StrategyDetailsDeployment represents the parsed details of a Deployment
// InstallStrategy.
type StrategyDetailsDeployment struct {
	DeploymentSpecs []v1beta1.DeploymentSpec        `json:"deployments"`
	Permissions     []StrategyDeploymentPermissions `json:"permissions"`
}

func (d *StrategyDetailsDeployment) Install(
	client client.Interface,
	owner metav1.ObjectMeta,
	ownerType metav1.TypeMeta,
) error {
	for _, permission := range d.Permissions {
		// create role
		role := &rbac.Role{
			Rules: permission.Rules,
		}
		role.SetGenerateName(fmt.Sprintf("%s-role-", owner.Name))
		createdRole, err := client.KubernetesInterface().RbacV1beta1().Roles(owner.Namespace).Create(role)
		if err != nil {
			return err
		}

		// create serviceaccount if necessary
		serviceAccount := &v1.ServiceAccount{}
		serviceAccount.SetName(permission.ServiceAccountName)
		serviceAccount, err = client.KubernetesInterface().CoreV1().ServiceAccounts(owner.Namespace).Create(serviceAccount)
		if err != nil && !errors.IsAlreadyExists(err) {
			return err
		}

		// create rolebinding
		roleBinding := &rbac.RoleBinding{
			RoleRef: rbac.RoleRef{
				Kind:     "Role",
				Name:     createdRole.GetName(),
				APIGroup: rbac.GroupName},
			Subjects: []rbac.Subject{{
				Kind:      "ServiceAccount",
				Name:      permission.ServiceAccountName,
				Namespace: owner.Namespace,
			}},
		}
		roleBinding.SetGenerateName(fmt.Sprintf("%s-%s-rolebinding-", createdRole.Name, serviceAccount.Name))

		if _, err = client.KubernetesInterface().RbacV1beta1().RoleBindings(owner.Namespace).Create(roleBinding); err != nil {
			return err
		}
	}

	for _, spec := range d.DeploymentSpecs {
		ownerReferences := []metav1.OwnerReference{
			{
				APIVersion:         apis.GroupName,
				Kind:               v1alpha1.ClusterServiceVersionKind,
				Name:               owner.GetName(),
				UID:                owner.UID,
				Controller:         &Controller,
				BlockOwnerDeletion: &BlockOwnerDeletion,
			},
		}
		dep := v1beta1.Deployment{Spec: spec}
		dep.SetNamespace(owner.Namespace)
		dep.SetOwnerReferences(ownerReferences)
		dep.SetGenerateName(fmt.Sprintf("%s-", owner.Name))
{'fraction_non_alphanumeric': 0.11835541028728712, 'fraction_numerical': 0.009805608119731636, 'mean_word_length': 3.873428331936295, 'pattern_counts': {'":': 0, '<': 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': '5591732', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1823, 'n_words': 331}