func CreateControllerProxy(
	config ControllerProxyConfig,
	labels labels.Set,
	configI core.ConfigMapInterface,
	roleI rbac.RoleInterface,
	roleBindingI rbac.RoleBindingInterface,
	saI core.ServiceAccountInterface,
) error {
	role := &rbacv1.Role{
		ObjectMeta: meta.ObjectMeta{
			Labels: labels,
			Name:   config.Name,
		},
		Rules: []rbacv1.PolicyRule{
			{
				APIGroups: []string{""},
				Resources: []string{"pods"},
				Verbs:     []string{"list", "get", "watch"},
			},
			{
				APIGroups: []string{""},
				Resources: []string{"services"},
				Verbs:     []string{"get"},
			},
			// The get verb below is not used directly by juju but is for
			// the python lib
			{
				APIGroups: []string{""},
				Resources: []string{"pods/portforward"},
				Verbs:     []string{"create", "get"},
			},
		},
	}

	role, err := roleI.Create(context.TODO(), role, meta.CreateOptions{})
	if err != nil {
		return fmt.Errorf("creating proxy service account role: %w", err)
	}

	sa := &corev1.ServiceAccount{
		ObjectMeta: meta.ObjectMeta{
			Labels: labels,
			Name:   config.Name,
		},
	}

	sa, err = saI.Create(context.TODO(), sa, meta.CreateOptions{})
	if err != nil {
		return fmt.Errorf("creating proxy service account: %w", err)
	}

	roleBinding := &rbacv1.RoleBinding{
		ObjectMeta: meta.ObjectMeta{
			Labels: labels,
			Name:   config.Name,
		},
		Subjects: []rbacv1.Subject{
			{
				Kind:      "ServiceAccount",
				Name:      sa.Name,
				Namespace: sa.Namespace,
			},
		},
		RoleRef: rbacv1.RoleRef{
			APIGroup: "rbac.authorization.k8s.io",
			Kind:     "Role",
			Name:     role.Name,
		},
	}

	_, err = roleBindingI.Create(context.TODO(), roleBinding, meta.CreateOptions{})
	if err != nil {
		return fmt.Errorf("creating proxy service account role binding: %w", err)
	}

	configJSON, err := json.Marshal(config)
	if err != nil {
		return fmt.Errorf("marshalling proxy configmap data to json: %w", err)
	}

	cm := &corev1.ConfigMap{
		ObjectMeta: meta.ObjectMeta{
			Labels: labels,
			Name:   config.Name,
		},
		Data: map[string]string{
			ProxyConfigMapKey: string(configJSON),
		},
	}

	_, err = configI.Create(context.TODO(), cm, meta.CreateOptions{})
	if err != nil {
		return fmt.Errorf("creating proxy config map: %w", err)
	}

	return nil
}
// CreateControllerProxy establishes the Kubernetes resources needed for
// proxying to a Juju controller. The end result of this function is a service
// account with a set of permissions that the Juju client can use for proxying
// to a controller.
CreateControllerProxy establishes the Kubernetes resources needed for
proxying to a Juju controller. The end result of this function is a service
account with a set of permissions that the Juju client can use for proxying
to a controller.
// CreateControllerProxy establishes the Kubernetes resources needed for // proxying to a Juju controller . The end result of this function is a service // account with a set of permissions that the Juju client can use for proxying // to a controller .
func CreateControllerProxy(
	config ControllerProxyConfig,
	labels labels.Set,
	configI core.ConfigMapInterface,
	roleI rbac.RoleInterface,
	roleBindingI rbac.RoleBindingInterface,
	saI core.ServiceAccountInterface,
) error {
	role := &rbacv1.Role{
		ObjectMeta: meta.ObjectMeta{
			Labels: labels,
			Name:   config.Name,
		},
		Rules: []rbacv1.PolicyRule{
			{
				APIGroups: []string{""},
				Resources: []string{"pods"},
				Verbs:     []string{"list", "get", "watch"},
			},
			{
				APIGroups: []string{""},
				Resources: []string{"services"},
				Verbs:     []string{"get"},
			},
			{
				APIGroups: []string{""},
				Resources: []string{"pods/portforward"},
				Verbs:     []string{"create", "get"},
			},
		},
	}
	role, err := roleI.Create(context.TODO(), role, meta.CreateOptions{})
	if err != nil {
		return fmt.Errorf("creating proxy service account role: %w", err)
	}
	sa := &corev1.ServiceAccount{
		ObjectMeta: meta.ObjectMeta{
			Labels: labels,
			Name:   config.Name,
		},
	}
	sa, err = saI.Create(context.TODO(), sa, meta.CreateOptions{})
	if err != nil {
		return fmt.Errorf("creating proxy service account: %w", err)
	}
	roleBinding := &rbacv1.RoleBinding{
		ObjectMeta: meta.ObjectMeta{
			Labels: labels,
			Name:   config.Name,
		},
		Subjects: []rbacv1.Subject{
			{
				Kind:      "ServiceAccount",
				Name:      sa.Name,
				Namespace: sa.Namespace,
			},
		},
		RoleRef: rbacv1.RoleRef{
			APIGroup: "rbac.authorization.k8s.io",
			Kind:     "Role",
			Name:     role.Name,
		},
	}
	_, err = roleBindingI.Create(context.TODO(), roleBinding, meta.CreateOptions{})
	if err != nil {
		return fmt.Errorf("creating proxy service account role binding: %w", err)
	}
	configJSON, err := json.Marshal(config)
	if err != nil {
		return fmt.Errorf("marshalling proxy configmap data to json: %w", err)
	}
	cm := &corev1.ConfigMap{
		ObjectMeta: meta.ObjectMeta{
			Labels: labels,
			Name:   config.Name,
		},
		Data: map[string]string{
			ProxyConfigMapKey: string(configJSON),
		},
	}
	_, err = configI.Create(context.TODO(), cm, meta.CreateOptions{})
	if err != nil {
		return fmt.Errorf("creating proxy config map: %w", err)
	}
	return nil
}
func CreateControllerProxy ( config ControllerProxyConfig , labels labels . Set , configI core . ConfigMapInterface , roleI rbac . RoleInterface , roleBindingI rbac . RoleBindingInterface , saI core . ServiceAccountInterface , ) error { role := & rbacv1 . Role { ObjectMeta : meta . ObjectMeta { Labels : labels , Name : config . Name , } , Rules : [ ] rbacv1 . PolicyRule { { APIGroups : [ ] string { " " } , Resources : [ ] string { " " } , Verbs : [ ] string { " " , " " , " " } , } , { APIGroups : [ ] string { " " } , Resources : [ ] string { " " } , Verbs : [ ] string { " " } , } , { APIGroups : [ ] string { " " } , Resources : [ ] string { " " } , Verbs : [ ] string { " " , " " } , } , } , } role , err := roleI . Create ( context . TODO ( ) , role , meta . CreateOptions { } ) if err != nil { return fmt . Errorf ( " " , err ) } sa := & corev1 . ServiceAccount { ObjectMeta : meta . ObjectMeta { Labels : labels , Name : config . Name , } , } sa , err = saI . Create ( context . TODO ( ) , sa , meta . CreateOptions { } ) if err != nil { return fmt . Errorf ( " " , err ) } roleBinding := & rbacv1 . RoleBinding { ObjectMeta : meta . ObjectMeta { Labels : labels , Name : config . Name , } , Subjects : [ ] rbacv1 . Subject { { Kind : " " , Name : sa . Name , Namespace : sa . Namespace , } , } , RoleRef : rbacv1 . RoleRef { APIGroup : " " , Kind : " " , Name : role . Name , } , } _ , err = roleBindingI . Create ( context . TODO ( ) , roleBinding , meta . CreateOptions { } ) if err != nil { return fmt . Errorf ( " " , err ) } configJSON , err := json . Marshal ( config ) if err != nil { return fmt . Errorf ( " " , err ) } cm := & corev1 . ConfigMap { ObjectMeta : meta . ObjectMeta { Labels : labels , Name : config . Name , } , Data : map [ string ] string { ProxyConfigMapKey : string ( configJSON ) , } , } _ , err = configI . Create ( context . TODO ( ) , cm , meta . CreateOptions { } ) if err != nil { return fmt . Errorf ( " " , err ) } return nil }
CreateControllerProxy establishes the Kubernetes resources needed for
proxying to a Juju controller.
CreateControllerProxy establishes the Kubernetes resources needed for proxying to a Juju controller .
// The get verb below is not used directly by juju but is for // the python lib