From 38d1336ea3435e952fcfa1c5a294d104682d317e Mon Sep 17 00:00:00 2001 From: "michal.gubricky" Date: Wed, 28 Feb 2024 14:56:56 +0100 Subject: [PATCH 1/2] Add fake github client Signed-off-by: michal.gubricky --- Tiltfile | 5 +- cmd/main.go | 23 ++++--- config/localmode/kustomization.yaml | 10 +++ .../localmode/manager_auth_proxy_patch.yaml | 40 +++++++++++ config/localmode/manager_config_patch.yaml | 15 ++++ config/localmode/manager_pull_policy.yaml | 11 +++ .../pkg/github/client/fake/github_client.go | 69 +++++++++++++++++++ vendor/modules.txt | 1 + 8 files changed, 164 insertions(+), 10 deletions(-) create mode 100644 config/localmode/kustomization.yaml create mode 100644 config/localmode/manager_auth_proxy_patch.yaml create mode 100644 config/localmode/manager_config_patch.yaml create mode 100644 config/localmode/manager_pull_policy.yaml create mode 100644 vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client/fake/github_client.go diff --git a/Tiltfile b/Tiltfile index f7e97486..c64d05c9 100644 --- a/Tiltfile +++ b/Tiltfile @@ -188,7 +188,10 @@ else: # Build cspo and add feature gates def deploy_cspo(): # yaml = str(kustomizesub("./hack/observability")) # build an observable kind deployment by default - yaml = str(kustomizesub("./config/default")) + if settings.get("local_mode"): + yaml = str(kustomizesub("./config/localmode")) + else: + yaml = str(kustomizesub("./config/default")) local_resource( name = "cspo-components", cmd = ["sh", "-ec", sed_cmd, yaml, "|", envsubst_cmd], diff --git a/cmd/main.go b/cmd/main.go index 247ffb3d..8e573977 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -24,6 +24,7 @@ import ( "time" githubclient "github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client" + "github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client/fake" apiv1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" "github.com/sovereignCloudStack/cluster-stack-provider-openstack/internal/controller" "k8s.io/apimachinery/pkg/runtime" @@ -49,17 +50,15 @@ func init() { } var ( - releaseDir string - imageImportTimeout int + releaseDir string + imageImportTimeout int + localMode bool + metricsAddr string + enableLeaderElection bool + probeAddr string ) func main() { - var ( - metricsAddr string - enableLeaderElection bool - probeAddr string - ) - flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") flag.BoolVar( @@ -70,6 +69,7 @@ func main() { ) flag.StringVar(&releaseDir, "release-dir", "/tmp/downloads/", "Specify release directory for cluster-stack releases") flag.IntVar(&imageImportTimeout, "image-import-timeout", 0, "Maximum time in minutes that you allow cspo to import image. If image-import-timeout <= 0, cspo waits forever.") + flag.BoolVar(&localMode, "local", false, "Enable local mode where no release assets will be downloaded from a remote Git repository. Useful for implementing cluster stacks.") opts := zap.Options{ Development: true, @@ -100,7 +100,12 @@ func main() { // Initialize event recorder. record.InitFromRecorder(mgr.GetEventRecorderFor("cspo-controller")) - gitFactory := githubclient.NewFactory() + var gitFactory githubclient.Factory + if localMode { + gitFactory = fake.NewFactory() + } else { + gitFactory = githubclient.NewFactory() + } if err = (&controller.OpenStackClusterStackReleaseReconciler{ Client: mgr.GetClient(), diff --git a/config/localmode/kustomization.yaml b/config/localmode/kustomization.yaml new file mode 100644 index 00000000..5012e20f --- /dev/null +++ b/config/localmode/kustomization.yaml @@ -0,0 +1,10 @@ +namespace: cspo-system + +namePrefix: cspo- +resources: +- ../crd +- ../rbac +- ../manager +patchesStrategicMerge: +- manager_auth_proxy_patch.yaml +- manager_config_patch.yaml diff --git a/config/localmode/manager_auth_proxy_patch.yaml b/config/localmode/manager_auth_proxy_patch.yaml new file mode 100644 index 00000000..01c016d3 --- /dev/null +++ b/config/localmode/manager_auth_proxy_patch.yaml @@ -0,0 +1,40 @@ +# This patch inject a sidecar container which is a HTTP proxy for the +# controller manager, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews. +apiVersion: apps/v1 +kind: Deployment +metadata: + name: controller-manager + namespace: system +spec: + template: + spec: + containers: + - name: kube-rbac-proxy + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - "ALL" + image: gcr.io/kubebuilder/kube-rbac-proxy:v0.15.0 + args: + - "--secure-listen-address=0.0.0.0:8443" + - "--upstream=http://127.0.0.1:8080/" + - "--logtostderr=true" + - "--v=0" + ports: + - containerPort: 8443 + protocol: TCP + name: https + resources: + limits: + cpu: 500m + memory: 128Mi + requests: + cpu: 5m + memory: 64Mi + - name: manager + args: + - "--health-probe-bind-address=:8081" + - "--metrics-bind-address=127.0.0.1:8080" + - "--leader-elect" + - "--local=true" diff --git a/config/localmode/manager_config_patch.yaml b/config/localmode/manager_config_patch.yaml new file mode 100644 index 00000000..837bccc8 --- /dev/null +++ b/config/localmode/manager_config_patch.yaml @@ -0,0 +1,15 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: controller-manager + namespace: system +spec: + template: + spec: + containers: + - image: ghcr.io/sovereigncloudstack/cspo-staging:dev + name: manager + args: + - --leader-elect=true + - --release-dir=/tmp + - --local=true diff --git a/config/localmode/manager_pull_policy.yaml b/config/localmode/manager_pull_policy.yaml new file mode 100644 index 00000000..74a0879c --- /dev/null +++ b/config/localmode/manager_pull_policy.yaml @@ -0,0 +1,11 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: controller-manager + namespace: system +spec: + template: + spec: + containers: + - name: manager + imagePullPolicy: Always diff --git a/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client/fake/github_client.go b/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client/fake/github_client.go new file mode 100644 index 00000000..90e0bd09 --- /dev/null +++ b/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client/fake/github_client.go @@ -0,0 +1,69 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package fake defines a fake Gitub client. +package fake + +import ( + "context" + "net/http" + + "github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client" + "github.com/go-logr/logr" + "github.com/google/go-github/v52/github" + "sigs.k8s.io/controller-runtime/pkg/log" +) + +type fakeClient struct { + log logr.Logger +} + +type factory struct{} + +var _ = client.Client(&fakeClient{}) + +var _ = client.Factory(&factory{}) + +// NewFactory returns a new factory for Github clients. +func NewFactory() client.Factory { + return &factory{} +} + +func (*factory) NewClient(ctx context.Context) (client.Client, error) { + logger := log.FromContext(ctx) + + return &fakeClient{ + log: logger, + }, nil +} + +func (c *fakeClient) ListRelease(_ context.Context) ([]*github.RepositoryRelease, *github.Response, error) { + c.log.Info("WARNING: called ListRelease of fake Github client") + resp := &github.Response{Response: &http.Response{StatusCode: http.StatusOK}} + return nil, resp, nil +} + +func (c *fakeClient) GetReleaseByTag(_ context.Context, _ string) (*github.RepositoryRelease, *github.Response, error) { + c.log.Info("WARNING: called GetReleaseByTag of fake Github client") + resp := &github.Response{Response: &http.Response{StatusCode: http.StatusOK}} + return nil, resp, nil +} + +// DownloadReleaseAssets downloads a list of release assets. +func (c *fakeClient) DownloadReleaseAssets(_ context.Context, _ *github.RepositoryRelease, _ string, _ []string) error { + c.log.Info("WARNING: called DownloadReleaseAssets of fake Github client") + return nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 7abe3749..4a5f8055 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -22,6 +22,7 @@ github.com/ProtonMail/go-crypto/openpgp/s2k ## explicit; go 1.19 github.com/SovereignCloudStack/cluster-stack-operator/pkg/clusterstack github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client +github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client/fake github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client/mocks github.com/SovereignCloudStack/cluster-stack-operator/pkg/kubernetesversion github.com/SovereignCloudStack/cluster-stack-operator/pkg/release From 4f6eb60c7bc27245b1b809ff2248e5f5cbab926f Mon Sep 17 00:00:00 2001 From: "michal.gubricky" Date: Thu, 29 Feb 2024 08:17:50 +0100 Subject: [PATCH 2/2] Remove release-dir from args in manager_config_patch for local_mode Signed-off-by: michal.gubricky --- config/localmode/manager_config_patch.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/config/localmode/manager_config_patch.yaml b/config/localmode/manager_config_patch.yaml index 837bccc8..b81a57e0 100644 --- a/config/localmode/manager_config_patch.yaml +++ b/config/localmode/manager_config_patch.yaml @@ -11,5 +11,4 @@ spec: name: manager args: - --leader-elect=true - - --release-dir=/tmp - --local=true