Skip to content

Commit 0e024d4

Browse files
committed
replace fields with enum
1 parent 3bcde90 commit 0e024d4

3 files changed

Lines changed: 46 additions & 34 deletions

File tree

crates/stackable-operator/crds/DummyCluster.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ spec:
7676
description: A validated domain name type conforming to RFC 1123, so e.g. not an IP address
7777
type: string
7878
gitSync:
79+
anyOf:
80+
- required:
81+
- credentialsSecret
82+
- required:
83+
- sshSecret
7984
properties:
8085
branch:
8186
default: main
@@ -91,10 +96,8 @@ spec:
9196
The referenced Secret must include two fields: `user` and `password`.
9297
The `password` field can either be an actual password (not recommended) or a GitHub token,
9398
as described in the git-sync [documentation].
94-
This cannot be provided if `ssh_secret` is also provided.
9599
96100
[documentation]: https://github.com/kubernetes/git-sync/tree/v4.2.4?tab=readme-ov-file#manual
97-
nullable: true
98101
type: string
99102
depth:
100103
default: 1
@@ -131,10 +134,8 @@ spec:
131134
The name of the Secret used for SSH access to the repository.
132135
133136
The referenced Secret must include two fields: `key` and `knownHosts`.
134-
This cannot be provided if `credentials_secret` is also provided.
135137
136138
[documentation]: https://github.com/kubernetes/git-sync/tree/v4.2.4?tab=readme-ov-file#manual
137-
nullable: true
138139
type: string
139140
wait:
140141
default: 20s

crates/stackable-operator/src/crd/git_sync/mod.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,6 @@ pub mod versioned {
4646
#[serde(default = "GitSync::default_wait")]
4747
pub wait: Duration,
4848

49-
/// The name of the Secret used to access the repository if it is not public.
50-
///
51-
/// The referenced Secret must include two fields: `user` and `password`.
52-
/// The `password` field can either be an actual password (not recommended) or a GitHub token,
53-
/// as described in the git-sync [documentation].
54-
/// This cannot be provided if `ssh_secret` is also provided.
55-
///
56-
/// [documentation]: https://github.com/kubernetes/git-sync/tree/v4.2.4?tab=readme-ov-file#manual
57-
pub credentials_secret: Option<String>,
58-
5949
/// A map of optional configuration settings that are listed in the git-sync [documentation].
6050
///
6151
/// Also read the git-sync [example] in our documentation. These settings are not verified.
@@ -65,12 +55,36 @@ pub mod versioned {
6555
#[serde(default)]
6656
pub git_sync_conf: BTreeMap<String, String>,
6757

68-
/// The name of the Secret used for SSH access to the repository.
69-
///
70-
/// The referenced Secret must include two fields: `key` and `knownHosts`.
71-
/// This cannot be provided if `credentials_secret` is also provided.
72-
///
73-
/// [documentation]: https://github.com/kubernetes/git-sync/tree/v4.2.4?tab=readme-ov-file#manual
74-
pub ssh_secret: Option<String>,
58+
#[serde(flatten)]
59+
pub access_secret: Option<AccessSecret>,
60+
}
61+
62+
#[derive(strum::Display, Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
63+
#[serde(untagged)]
64+
#[serde(rename_all = "camelCase")]
65+
#[schemars(rename_all = "camelCase")]
66+
pub enum AccessSecret {
67+
Credentials {
68+
/// The name of the Secret used to access the repository if it is not public.
69+
///
70+
/// The referenced Secret must include two fields: `user` and `password`.
71+
/// The `password` field can either be an actual password (not recommended) or a GitHub token,
72+
/// as described in the git-sync [documentation].
73+
///
74+
/// [documentation]: https://github.com/kubernetes/git-sync/tree/v4.2.4?tab=readme-ov-file#manual
75+
#[serde(rename = "credentialsSecret")]
76+
#[schemars(rename = "credentialsSecret")]
77+
credentials_secret: String,
78+
},
79+
Ssh {
80+
/// The name of the Secret used for SSH access to the repository.
81+
///
82+
/// The referenced Secret must include two fields: `key` and `knownHosts`.
83+
///
84+
/// [documentation]: https://github.com/kubernetes/git-sync/tree/v4.2.4?tab=readme-ov-file#manual
85+
#[serde(rename = "sshSecret")]
86+
#[schemars(rename = "sshSecret")]
87+
ssh_secret: String,
88+
},
7589
}
7690
}

crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
volume::{VolumeBuilder, VolumeMountBuilder},
1515
},
1616
commons::product_image_selection::ResolvedProductImage,
17-
crd::git_sync::v1alpha1::GitSync,
17+
crd::git_sync::v1alpha1::{AccessSecret, GitSync},
1818
product_config_utils::insert_or_update_env_vars,
1919
product_logging::{
2020
framework::capture_shell_output,
@@ -117,25 +117,22 @@ impl GitSyncResources {
117117
let mut resources = GitSyncResources::default();
118118

119119
for (i, git_sync) in git_syncs.iter().enumerate() {
120-
if git_sync.credentials_secret.is_some() && git_sync.ssh_secret.is_some() {
121-
// Gitsync will not allow the declaration of both ssh-key and password/token credentials
122-
return Err(Error::MultipleCredentials);
123-
}
124-
125120
let mut env_vars = vec![];
126-
if let Some(git_credentials_secret) = &git_sync.credentials_secret {
121+
122+
if let Some(AccessSecret::Credentials { credentials_secret }) = &git_sync.access_secret
123+
{
127124
env_vars.push(GitSyncResources::env_var_from_secret(
128125
"GITSYNC_USERNAME",
129-
git_credentials_secret,
126+
credentials_secret,
130127
"user",
131128
));
132129
env_vars.push(GitSyncResources::env_var_from_secret(
133130
"GITSYNC_PASSWORD",
134-
git_credentials_secret,
131+
credentials_secret,
135132
"password",
136133
));
137134
}
138-
if git_sync.ssh_secret.is_some() {
135+
if matches!(git_sync.access_secret, Some(AccessSecret::Ssh { .. })) {
139136
env_vars.push(EnvVar {
140137
name: "GITSYNC_SSH_KEY_FILE".to_owned(),
141138
value: Some(format!("{SSH_MOUNT_PATH_PREFIX}-{i}/key").to_owned()),
@@ -170,7 +167,7 @@ impl GitSyncResources {
170167

171168
git_sync_container_volume_mounts.extend_from_slice(extra_volume_mounts);
172169

173-
if git_sync.ssh_secret.is_some() {
170+
if matches!(git_sync.access_secret, Some(AccessSecret::Ssh { .. })) {
174171
let ssh_mount_path = format!("{SSH_MOUNT_PATH_PREFIX}-{i}");
175172
let ssh_volume_name = format!("{SSH_VOLUME_NAME_PREFIX}-{i}");
176173

@@ -225,11 +222,11 @@ impl GitSyncResources {
225222
.push(git_content_volume_mount);
226223
resources.git_content_folders.push(git_content_folder);
227224

228-
if let Some(get_ssh_secret) = &git_sync.ssh_secret {
225+
if let Some(AccessSecret::Ssh { ssh_secret }) = &git_sync.access_secret {
229226
let ssh_volume_name = format!("{SSH_VOLUME_NAME_PREFIX}-{i}");
230227

231228
let ssh_secret_volume = VolumeBuilder::new(&ssh_volume_name)
232-
.with_secret(get_ssh_secret, false)
229+
.with_secret(ssh_secret, false)
233230
.build();
234231
resources.git_ssh_volumes.push(ssh_secret_volume);
235232
}

0 commit comments

Comments
 (0)