Skip to content

Commit f3f158f

Browse files
Openshift support (#285)
* adding scc as part of cluster-resources dir * Added an scc that will contain all needed SA's for the runtime * fixes according to code review * fix * bump
1 parent ed48d47 commit f3f158f

File tree

10 files changed

+158
-6
lines changed

10 files changed

+158
-6
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.262
1+
VERSION=v0.0.263
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/runtime.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import (
5555
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
5656
argocdv1alpha1cs "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned"
5757
aev1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1"
58+
oc "github.com/codefresh-io/cli-v2/pkg/util/openshift"
5859

5960
"github.com/Masterminds/semver/v3"
6061
kubeutil "github.com/codefresh-io/cli-v2/pkg/util/kube"
@@ -604,6 +605,15 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
604605
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to bootstrap repository: %w", err))
605606
}
606607

608+
err = oc.PrepareOpenshiftCluster(ctx, &oc.OpenshiftOptions{
609+
KubeFactory: opts.KubeFactory,
610+
RuntimeName: opts.RuntimeName,
611+
InsCloneOpts: opts.InsCloneOpts,
612+
})
613+
if err != nil {
614+
return fmt.Errorf("failed setting up environment for openshift %w", err)
615+
}
616+
607617
err = apcmd.RunProjectCreate(ctx, &apcmd.ProjectCreateOptions{
608618
CloneOpts: opts.InsCloneOpts,
609619
ProjectName: opts.RuntimeName,

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.262/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.263/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.262/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.263/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/juju/ansiterm v0.0.0-20210929141451-8b71cc96ebdc
2222
github.com/manifoldco/promptui v0.8.0
2323
github.com/mattn/go-colorable v0.1.12 // indirect
24+
github.com/openshift/api v3.9.0+incompatible
2425
github.com/rkrmr33/checklist v0.0.5
2526
github.com/segmentio/backo-go v1.0.0 // indirect
2627
github.com/sirupsen/logrus v1.8.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,8 @@ github.com/opencontainers/runc v1.0.0-rc93/go.mod h1:3NOsor4w32B2tC0Zbl8Knk4Wg84
10531053
github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
10541054
github.com/opencontainers/runtime-spec v1.0.3-0.20200929063507-e6143ca7d51d/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
10551055
github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3ogry1nUQF8Evvo=
1056+
github.com/openshift/api v3.9.0+incompatible h1:fJ/KsefYuZAjmrr3+5U9yZIZbTOpVkDDLDLFresAeYs=
1057+
github.com/openshift/api v3.9.0+incompatible/go.mod h1:dh9o4Fs58gpFXGSYfnVxGR9PnV53I8TW84pQaJDdGiY=
10561058
github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis=
10571059
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
10581060
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
apiVersion: kustomize.config.k8s.io/v1beta1
22
kind: Kustomization
33
resources:
4-
- https://raw.githubusercontent.com/codefresh-io/argo-events/v1.5.5-cap-CR-8983/manifests/install.yaml
5-
- https://raw.githubusercontent.com/codefresh-io/argo-events/v1.5.5-cap-CR-8983/manifests/install-validating-webhook.yaml
4+
- https://raw.githubusercontent.com/codefresh-io/argo-events/v1.5.5-cap-CR-9720/manifests/install.yaml
5+
- https://raw.githubusercontent.com/codefresh-io/argo-events/v1.5.5-cap-CR-9720/manifests/install-validating-webhook.yaml
66
- eventbus.yaml

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.1
8-
version: 0.0.262
8+
version: 0.0.263
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/store/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type Store struct {
132132
NetworkTesterImage string
133133
MinKubeVersion string
134134
MaxKubeVersion string
135+
SccName string
135136
CFInternalGitSources []string
136137
CFInternalReporters []string
137138
}
@@ -223,6 +224,7 @@ func init() {
223224
s.NetworkTesterImage = "quay.io/codefresh/cf-venona-network-tester:latest"
224225
s.MinKubeVersion = "v1.18.0"
225226
s.MaxKubeVersion = "v1.21.9"
227+
s.SccName = "cf-scc"
226228
s.CFInternalGitSources = []string{s.MarketplaceGitSourceName}
227229
s.CFInternalReporters = []string{s.EventsReporterName, s.WorkflowReporterName, s.RolloutReporterName}
228230

pkg/util/kube/kube.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
authv1 "k8s.io/api/authorization/v1"
2929
batchv1 "k8s.io/api/batch/v1"
3030
v1 "k8s.io/api/core/v1"
31+
"k8s.io/apimachinery/pkg/api/errors"
3132
"k8s.io/apimachinery/pkg/api/resource"
3233
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3334
"k8s.io/apimachinery/pkg/version"
@@ -421,3 +422,20 @@ func getPodLogs(ctx context.Context, client kubernetes.Interface, namespace, nam
421422

422423
return strings.Trim(logsBuf.String(), "\n"), nil
423424
}
425+
426+
func CheckNamespaceExists(ctx context.Context, namespace string, kubeFactory kube.Factory) (bool, error) {
427+
client, err := kubeFactory.KubernetesClientSet()
428+
if err != nil {
429+
return false, fmt.Errorf("failed to create kubernetes client: %w", err)
430+
}
431+
432+
_, err = client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
433+
if err != nil {
434+
if errors.IsNotFound(err) {
435+
return false, nil
436+
}
437+
return false, fmt.Errorf("failed to get namespace %s: %w", namespace, err)
438+
}
439+
440+
return true, nil
441+
}

pkg/util/openshift/util.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)