Skip to content

Commit

Permalink
use local_mode in Tiltfile
Browse files Browse the repository at this point in the history
Signed-off-by: Anurag <anurag.kumar@syself.com>
  • Loading branch information
Anurag committed Jan 11, 2024
1 parent 8d168d7 commit 6de6c0c
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ main
# helm generated yamls
*.tgz.yaml
*.build.yaml
.release
79 changes: 54 additions & 25 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ settings = {
"allowed_contexts": [
"kind-cso",
],
"local_mode": True,
"deploy_cert_manager": True,
"preload_images_for_kind": True,
"kind_cluster_name": "cso",
"capi_version": "v1.5.2",
"capi_version": "v1.6.0",
"cert_manager_version": "v1.11.0",
"kustomize_substitutions": {
},
Expand Down Expand Up @@ -111,31 +112,45 @@ def fixup_yaml_empty_arrays(yaml_str):
return yaml_str.replace("storedVersions: null", "storedVersions: []")

## This should have the same versions as the Dockerfile
tilt_dockerfile_header_cso = """
FROM docker.io/alpine/helm:3.12.2 as helm
FROM docker.io/library/alpine:3.18.0 as tilt
WORKDIR /
COPY --from=helm --chown=root:root --chmod=755 /usr/bin/helm /usr/local/bin/helm
COPY manager .
"""
if settings.get("local_mode"):
tilt_dockerfile_header_cso = """
FROM docker.io/alpine/helm:3.12.2 as helm
FROM docker.io/library/alpine:3.18.0 as tilt
WORKDIR /
COPY --from=helm --chown=root:root --chmod=755 /usr/bin/helm /usr/local/bin/helm
COPY .tiltbuild/manager .
COPY .release/ /tmp/cluster-stacks/
"""
else:
tilt_dockerfile_header_cso = """
FROM docker.io/alpine/helm:3.12.2 as helm
FROM docker.io/library/alpine:3.18.0 as tilt
WORKDIR /
COPY --from=helm --chown=root:root --chmod=755 /usr/bin/helm /usr/local/bin/helm
COPY manager .
"""

# Build CSO and add feature gates
def deploy_cso():
# 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 = "cso-components",
cmd = ["sh", "-ec", sed_cmd, yaml, "|", envsubst_cmd],
labels = ["CSO"],
)

# Forge the build command
ldflags = "-extldflags \"-static\" " + str(local("hack/version.sh")).rstrip("\n")
# ldflags = "-extldflags \"-static\" " + str(local("hack/version.sh")).rstrip("\n")
build_env = "CGO_ENABLED=0 GOOS=linux GOARCH=amd64"
build_cmd = "{build_env} go build -ldflags '{ldflags}' -o .tiltbuild/manager cmd/main.go".format(
build_cmd = "{build_env} go build -o .tiltbuild/manager cmd/main.go".format(
build_env = build_env,
ldflags = ldflags,
# ldflags = ldflags,
)
# Set up a local_resource build of the provider's manager binary.
local_resource(
Expand All @@ -152,18 +167,32 @@ def deploy_cso():

# Set up an image build for the provider. The live update configuration syncs the output from the local_resource
# build into the container.
docker_build_with_restart(
ref = "ghcr.io/sovereigncloudstack/cso-staging",
context = "./.tiltbuild/",
dockerfile_contents = tilt_dockerfile_header_cso,
target = "tilt",
entrypoint = entrypoint,
only = "manager",
live_update = [
sync(".tiltbuild/manager", "/manager"),
],
ignore = ["templates"],
)
if settings.get("local_mode"):
docker_build_with_restart(
ref = "ghcr.io/sovereigncloudstack/cso-staging",
context = ".",
dockerfile_contents = tilt_dockerfile_header_cso,
target = "tilt",
entrypoint = entrypoint,
live_update = [
sync(".tiltbuild/manager", "/manager"),
sync(".release", "/tmp/cluster-stacks"),
],
ignore = ["templates"],
)
else:
docker_build_with_restart(
ref = "ghcr.io/sovereigncloudstack/cso-staging",
context = "./.tiltbuild/",
dockerfile_contents = tilt_dockerfile_header_cso,
target = "tilt",
entrypoint = entrypoint,
live_update = [
sync(".tiltbuild/manager", "/manager"),
],
ignore = ["templates"],
)

k8s_yaml(blob(yaml))
k8s_resource(workload = "cso-controller-manager", labels = ["CSO"])
k8s_resource(
Expand Down
10 changes: 9 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/SovereignCloudStack/cluster-stack-operator/internal/controller"
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/csoversion"
githubclient "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/kube"
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/utillog"
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/workloadcluster"
Expand Down Expand Up @@ -67,6 +68,7 @@ var (
clusterAddonConcurrency int
logLevel string
releaseDir string
localMode bool
qps float64
burst int
)
Expand All @@ -83,6 +85,7 @@ func main() {
flag.IntVar(&clusterAddonConcurrency, "clusteraddon-concurrency", 1, "Number of ClusterAddons to process simultaneously")
flag.StringVar(&logLevel, "log-level", "info", "Specifies log level. Options are 'debug', 'info' and 'error'")
flag.StringVar(&releaseDir, "release-dir", "/tmp/downloads/", "Specify release directory for cluster-stack releases")
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.")
flag.Float64Var(&qps, "qps", 50, "Enable custom query per second for kubernetes API server")
flag.IntVar(&burst, "burst", 100, "Enable custom burst defines how many queries the API server will accept before enforcing the limit established by qps")

Expand Down Expand Up @@ -116,7 +119,12 @@ func main() {
// Setup the context that's going to be used in controllers and for the manager.
ctx := ctrl.SetupSignalHandler()

gitFactory := githubclient.NewFactory()
var gitFactory githubclient.Factory
if localMode {
gitFactory = fake.NewFactory()
} else {
gitFactory = githubclient.NewFactory()
}

restConfig := mgr.GetConfig()
restConfig.QPS = float32(qps)
Expand Down
2 changes: 1 addition & 1 deletion config/cso/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
cidrBlocks: ["192.168.0.0/16"]
serviceDomain: "cluster.local"
topology:
class: docker-ferrol-1-27-v1
class: docker-ferrol-1-27-v2
controlPlane:
metadata: {}
replicas: 1
Expand Down
2 changes: 1 addition & 1 deletion config/cso/clusterstack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ spec:
autoSubscribe: false
noProvider: true
versions:
- v1
- v2
43 changes: 43 additions & 0 deletions config/localmode/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace: cso-system

namePrefix: cso-

commonLabels:
cluster.x-k8s.io/provider: "infrastructure-cluster-stack-operator"

resources:
- ../crd
- ../rbac
- ../manager
- ../certmanager

patchesStrategicMerge:
- manager_config_patch.yaml
- manager_pull_policy.yaml
# vars:
# - name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
# objref:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
# fieldref:
# fieldpath: metadata.namespace
# - name: CERTIFICATE_NAME
# objref:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
# - name: SERVICE_NAMESPACE # namespace of the service
# objref:
# kind: Service
# version: v1
# name: webhook-service
# fieldref:
# fieldpath: metadata.namespace
# - name: SERVICE_NAME
# objref:
# kind: Service
# version: v1
# name: webhook-service
15 changes: 15 additions & 0 deletions config/localmode/manager_config_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- image: ghcr.io/sovereigncloudstack/cso-staging:dev
name: manager
args:
- --leader-elect=true
- --release-dir=/tmp
- --local=true
11 changes: 11 additions & 0 deletions config/localmode/manager_pull_policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
imagePullPolicy: Always
23 changes: 23 additions & 0 deletions config/localmode/manager_webhook_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
ports:
- containerPort: 9443
name: webhook-server
protocol: TCP
volumeMounts:
- mountPath: /tmp/k8s-webhook-server/serving-certs
name: cert
readOnly: true
volumes:
- name: cert
secret:
defaultMode: 420
secretName: cso-webhook-server-cert
9 changes: 9 additions & 0 deletions config/localmode/webhookcainjection_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This patch add annotation to admission webhook config and
# the variables $(CERTIFICATE_NAMESPACE) and $(CERTIFICATE_NAME) will be substituted by kustomize.
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: validating-webhook-configuration
annotations:
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
2 changes: 1 addition & 1 deletion hack/version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ version::get_version_vars() {
fi
fi

GIT_RELEASE_TAG=$(git describe --abbrev=0 --tags)
GIT_RELEASE_TAG=$(git describe --abbrev=0 --tags 2>/dev/null)
}

# borrowed from k8s.io/hack/lib/version.sh and modified
Expand Down
69 changes: 69 additions & 0 deletions pkg/github/client/fake/github_client.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6de6c0c

Please sign in to comment.