|
| 1 | +// Copyright 2022 The Codefresh Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package openshift |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/argoproj-labs/argocd-autopilot/pkg/git" |
| 22 | + "github.com/argoproj-labs/argocd-autopilot/pkg/kube" |
| 23 | + apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store" |
| 24 | + "github.com/codefresh-io/cli-v2/pkg/log" |
| 25 | + "github.com/codefresh-io/cli-v2/pkg/store" |
| 26 | + apu "github.com/codefresh-io/cli-v2/pkg/util/aputil" |
| 27 | + kubeutil "github.com/codefresh-io/cli-v2/pkg/util/kube" |
| 28 | + ocsecurityv1 "github.com/openshift/api/security/v1" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | +) |
| 31 | + |
| 32 | +type OpenshiftOptions struct { |
| 33 | + KubeFactory kube.Factory |
| 34 | + RuntimeName string |
| 35 | + InsCloneOpts *git.CloneOptions |
| 36 | +} |
| 37 | + |
| 38 | +const openshiftNs = "openshift" |
| 39 | + |
| 40 | +func PrepareOpenshiftCluster(ctx context.Context, opts *OpenshiftOptions) error { |
| 41 | + isOpenshift, err := isOpenshiftCluster(ctx, opts.KubeFactory) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + if !isOpenshift { |
| 47 | + return nil |
| 48 | + } |
| 49 | + |
| 50 | + err = createScc(ctx, opts) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func isOpenshiftCluster(ctx context.Context, kubeFactory kube.Factory) (bool, error) { |
| 59 | + exists, err := kubeutil.CheckNamespaceExists(ctx, openshiftNs, kubeFactory) |
| 60 | + if err != nil { |
| 61 | + return false, err |
| 62 | + } |
| 63 | + if !exists { |
| 64 | + return false, nil |
| 65 | + } |
| 66 | + |
| 67 | + log.G().Info("Running on an Openshift cluster") |
| 68 | + return true, nil |
| 69 | +} |
| 70 | + |
| 71 | +func createScc(ctx context.Context, opts *OpenshiftOptions) error { |
| 72 | + r, fs, err := opts.InsCloneOpts.GetRepo(ctx) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + sccPriority := int32(15) |
| 78 | + |
| 79 | + scc := ocsecurityv1.SecurityContextConstraints{ |
| 80 | + TypeMeta: metav1.TypeMeta{ |
| 81 | + Kind: "SecurityContextConstraints", |
| 82 | + APIVersion: "security.openshift.io/v1", |
| 83 | + }, |
| 84 | + ObjectMeta: metav1.ObjectMeta{ |
| 85 | + Namespace: opts.RuntimeName, |
| 86 | + Name: store.Get().SccName, |
| 87 | + }, |
| 88 | + AllowPrivilegedContainer: false, |
| 89 | + RunAsUser: ocsecurityv1.RunAsUserStrategyOptions{ |
| 90 | + Type: ocsecurityv1.RunAsUserStrategyRunAsAny, |
| 91 | + }, |
| 92 | + SELinuxContext: ocsecurityv1.SELinuxContextStrategyOptions{ |
| 93 | + Type: ocsecurityv1.SELinuxStrategyRunAsAny, |
| 94 | + }, |
| 95 | + Users: getServiceAccountsList(opts.RuntimeName), |
| 96 | + // This is required to take precedence over the default SCC's |
| 97 | + Priority: &sccPriority, |
| 98 | + } |
| 99 | + |
| 100 | + clusterResourcesDir := fs.Join(apstore.Default.BootsrtrapDir, apstore.Default.ClusterResourcesDir, "in-cluster") |
| 101 | + |
| 102 | + if err = fs.WriteYamls(fs.Join(clusterResourcesDir, "scc.yaml"), scc); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + log.G(ctx).Info("Pushing scc manifest") |
| 107 | + |
| 108 | + return apu.PushWithMessage(ctx, r, "Created scc") |
| 109 | +} |
| 110 | + |
| 111 | +func getServiceAccountsList(runtimeName string) []string { |
| 112 | + return []string{ |
| 113 | + fmt.Sprintf("system:serviceaccount:%s:argo-events-sa", runtimeName), |
| 114 | + fmt.Sprintf("system:serviceaccount:%s:argo-events-webhook-sa", runtimeName), |
| 115 | + fmt.Sprintf("system:serviceaccount:%s:argo-server", runtimeName), |
| 116 | + fmt.Sprintf("system:serviceaccount:%s:argocd-redis", runtimeName), |
| 117 | + fmt.Sprintf("system:serviceaccount:%s:cap-app-proxy", runtimeName), |
| 118 | + } |
| 119 | +} |
0 commit comments