Skip to content

Commit 77b4fc1

Browse files
Added in openshift.py task as well as support to pass in pull secret as a parameter
1 parent 019da76 commit 77b4fc1

6 files changed

Lines changed: 105 additions & 5 deletions

File tree

components/kubernetes/openshift.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
oscomp "github.com/DataDog/test-infra-definitions/components/os"
1212
"github.com/DataDog/test-infra-definitions/components/remote"
1313
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
14+
sdkconfig "github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
1415
)
1516

1617
func NewLocalOpenShiftCluster(env config.Env, name string, opts ...pulumi.ResourceOption) (*Cluster, error) {
@@ -57,17 +58,15 @@ func NewLocalOpenShiftCluster(env config.Env, name string, opts ...pulumi.Resour
5758
}
5859

5960
func NewOpenShiftCluster(env config.Env, vm *remote.Host, name string, opts ...pulumi.ResourceOption) (*Cluster, error) {
60-
pullSecretPath := os.Getenv("PULL_SECRET_PATH")
61-
if pullSecretPath == "" {
62-
return nil, fmt.Errorf("PULL_SECRET_PATH environment variable is not set")
63-
}
64-
6561
return components.NewComponent(env, name, func(clusterComp *Cluster) error {
6662
openShiftClusterName := env.CommonNamer().DisplayName(49)
6763
opts = utils.MergeOptions[pulumi.ResourceOption](opts, pulumi.Parent(clusterComp))
6864
runner := vm.OS.Runner()
6965
commonEnvironment := env
7066

67+
infraConfig := sdkconfig.New(env.Ctx(), "ddinfra")
68+
pullSecretPath := infraConfig.Require("openShiftPullSecretPath")
69+
7170
openShiftInstallBinary, err := InstallOpenShiftBinary(env, vm, opts...)
7271
if err != nil {
7372
return err

tasks/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Azure(BaseModel, extra=Extra.forbid):
4848
class GCP(BaseModel, extra=Extra.forbid):
4949
_DEFAULT_ACCOUNT = "agent-sandbox"
5050
publicKeyPath: Optional[str] = None
51+
pullSecretPath: Optional[str] = None
5152
account: Optional[str] = _DEFAULT_ACCOUNT
5253

5354
gcp: Optional[GCP] = None

tasks/doc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@
3737
agent_env: str = "Extra environment variables to run the agent with, the format is `VAR1=val1,VAR2=val2`"
3838
helm_config: str = "Path to a custom helm config file that will be merged with the default one"
3939
local_package: str = "Path to a local package to install, it can be either a folder or a file. If a folder is targetted we automatically take the first file with the correct extension depending on the targetted OS"
40+
pull_secret_path: str = "Path to the OpenShift pull secret file (required for OpenShift cluster creation)."

tasks/gcp/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
from tasks.gcp.gke import create_gke, destroy_gke
66
from tasks.gcp.vm import create_vm, destroy_vm
7+
from tasks.gcp.openshift import create_openshift, destroy_openshift
78

89
collection = Collection()
910
collection.add_task(destroy_vm)
1011
collection.add_task(create_vm)
1112
collection.add_task(create_gke)
1213
collection.add_task(destroy_gke)
14+
collection.add_task(create_openshift)
15+
collection.add_task(destroy_openshift)

tasks/gcp/openshift.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from typing import Optional
2+
3+
from invoke.context import Context
4+
from invoke.exceptions import Exit
5+
from invoke.tasks import task
6+
from pydantic_core._pydantic_core import ValidationError
7+
8+
from tasks import config, doc
9+
from tasks.config import get_full_profile_path
10+
from tasks.deploy import deploy
11+
from tasks.destroy import destroy
12+
13+
scenario_name = "gcp/openshiftvm"
14+
15+
16+
@task(
17+
help={
18+
"config_path": doc.config_path,
19+
"stack_name": doc.stack_name,
20+
"pull_secret_path": doc.pull_secret_path,
21+
}
22+
)
23+
def create_openshift(
24+
ctx: Context,
25+
config_path: Optional[str] = None,
26+
stack_name: Optional[str] = None,
27+
interactive: Optional[bool] = True,
28+
pull_secret_path: Optional[str] = None,
29+
):
30+
"""
31+
Create an OpenShift environment.
32+
"""
33+
34+
try:
35+
cfg = config.get_local_config(config_path)
36+
except ValidationError as e:
37+
raise Exit(f"Error in config {get_full_profile_path(config_path)}") from e
38+
39+
# Use parameter if provided during invoke setup, otherwise use config
40+
if not pull_secret_path:
41+
pull_secret_path = cfg.get_gcp().pullSecretPath
42+
if not pull_secret_path:
43+
raise Exit("pull_secret_path is required. Either use invoke.gcp.create-openshift -p <pull_secret_path> or configure it with 'invoke setup'")
44+
45+
extra_flags = {
46+
"scenario": scenario_name,
47+
"ddinfra:env": f"gcp/{cfg.get_gcp().account}",
48+
"ddinfra:gcp/defaultPublicKeyPath": cfg.get_gcp().publicKeyPath,
49+
"ddinfra:openShiftPullSecretPath": pull_secret_path,
50+
}
51+
52+
full_stack_name = deploy(
53+
ctx,
54+
scenario_name,
55+
config_path,
56+
stack_name=stack_name,
57+
install_agent=False,
58+
extra_flags=extra_flags,
59+
)
60+
61+
@task(
62+
help={
63+
"config_path": doc.config_path,
64+
"stack_name": doc.stack_name,
65+
}
66+
)
67+
def destroy_openshift(
68+
ctx: Context,
69+
config_path: Optional[str] = None,
70+
stack_name: Optional[str] = None,
71+
):
72+
"""
73+
Destroy an environment created by invoke gcp.create-openshift.
74+
"""
75+
destroy(
76+
ctx,
77+
scenario_name=scenario_name,
78+
config_path=config_path,
79+
stack=stack_name,
80+
)

tasks/setup.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,22 @@ def setup_gcp_config(config: Config):
328328
if default_account:
329329
config.configParams.gcp.account = default_account
330330

331+
# openShift pull secret path
332+
if config.configParams.gcp.pullSecretPath is None:
333+
config.configParams.gcp.pullSecretPath = ""
334+
default_pull_secret_path = config.configParams.gcp.pullSecretPath
335+
while True:
336+
config.configParams.gcp.pullSecretPath = default_pull_secret_path
337+
pull_secret_path = ask(
338+
f"🔑 Path to your OpenShift pull secret file (optional, can be set later): "
339+
)
340+
if pull_secret_path:
341+
config.configParams.gcp.pullSecretPath = pull_secret_path
342+
343+
if os.path.isfile(config.configParams.gcp.pullSecretPath):
344+
break
345+
warn(f"{config.configParams.gcp.pullSecretPath} is not a valid file")
346+
331347

332348
def setupAgentConfig(config):
333349
if config.configParams.agent is None:

0 commit comments

Comments
 (0)