Skip to content

Commit

Permalink
Merge branch '2333-timescaledb_tsl-extension-random-version-change' i…
Browse files Browse the repository at this point in the history
…nto 'main'

fix: SGConfig was missing spec.jobs.serviceAccount section and some field...

Closes #2333

See merge request ongresinc/stackgres!1500
  • Loading branch information
teoincontatto committed Dec 15, 2023
2 parents 550cc35 + 9864d3b commit 7cab40d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.stackgres.apiweb.dto.extension.Extension;
Expand Down Expand Up @@ -71,9 +72,11 @@ private Extension getExtension(StackGresExtension source, StackGresCluster clust
extension.setRepository(source.getRepository());
transformation.setVersions(
Seq.seq(extensionMetadataManager.getExtensionsAnyVersion(cluster, extension, false))
.map(StackGresExtensionMetadata::getVersion)
.map(StackGresExtensionVersion::getVersion)
.grouped(Function.identity())
.grouped(Function.<StackGresExtensionMetadata>identity()
.andThen(StackGresExtensionMetadata::getVersion)
.andThen(StackGresExtensionVersion::getVersion))
.map(t -> t.map2(Stream::toList))
.sorted(t -> t.v2.stream().sorted().findFirst().orElseThrow())
.map(Tuple2::v1)
.toList());
return transformation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -116,7 +117,7 @@ private List<StackGresExtensionMetadata> extractLatestVersions(
.grouped(Tuple3::limit2)
.map(group -> group.v2
.map(Tuple3::v3)
.max(StackGresExtensionMetadata::compareBuild)
.max(Comparator.comparing(StackGresExtensionMetadata::getBuild))
.orElseThrow())
.toUnmodifiableList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@
package io.stackgres.common.extension;

import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.stackgres.common.StackGresUtil;
import io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension;

public class StackGresExtensionMetadata {

private static final Pattern BUILD_PATTERN = Pattern.compile(
"^(?<major>[0-9]+)\\.(?<minor>[0-9]+)(?:-dev)?$");
public class StackGresExtensionMetadata
implements Comparable<StackGresExtensionMetadata> {

private final StackGresExtension extension;
private final StackGresExtensionVersion version;
private final StackGresExtensionVersionTarget target;
private final StackGresExtensionPublisher publisher;
private final Integer majorBuild;
private final Integer minorBuild;
private final StackGresExtensionMetadataBuild build;

public StackGresExtensionMetadata(StackGresExtension extension,
StackGresExtensionVersion version, StackGresExtensionVersionTarget target,
Expand All @@ -33,17 +28,7 @@ public StackGresExtensionMetadata(StackGresExtension extension,
this.version = version;
this.target = target;
this.publisher = publisher;
Optional<Matcher> matcher = Optional.ofNullable(target.getBuild())
.map(BUILD_PATTERN::matcher)
.filter(Matcher::find);
this.majorBuild = matcher
.map(m -> m.group("major"))
.map(Integer::parseInt)
.orElse(0);
this.minorBuild = matcher
.map(m -> m.group("minor"))
.map(Integer::parseInt)
.orElse(0);
this.build = new StackGresExtensionMetadataBuild(target.getBuild());
}

public StackGresExtensionMetadata(StackGresClusterInstalledExtension installedExtension) {
Expand All @@ -60,17 +45,7 @@ public StackGresExtensionMetadata(StackGresClusterInstalledExtension installedEx
this.target.setOs(ExtensionUtil.DEFAULT_OS);
this.target.setArch(ExtensionUtil.DEFAULT_ARCH);
this.publisher = new StackGresExtensionPublisher();
Optional<Matcher> matcher = Optional.ofNullable(installedExtension.getBuild())
.map(BUILD_PATTERN::matcher)
.filter(Matcher::find);
this.majorBuild = matcher
.map(m -> m.group("major"))
.map(Integer::parseInt)
.orElse(0);
this.minorBuild = matcher
.map(m -> m.group("minor"))
.map(Integer::parseInt)
.orElse(0);
this.build = new StackGresExtensionMetadataBuild(installedExtension.getBuild());
}

public StackGresExtension getExtension() {
Expand All @@ -81,12 +56,17 @@ public StackGresExtensionVersion getVersion() {
return version;
}

@JsonIgnore
public StackGresExtensionMetadataBuild getBuild() {
return build;
}

public int getMajorBuild() {
return majorBuild;
return build.getMajorBuild();
}

public int getMinorBuild() {
return minorBuild;
return build.getMinorBuild();
}

public StackGresExtensionVersionTarget getTarget() {
Expand Down Expand Up @@ -121,13 +101,28 @@ public String toString() {
return StackGresUtil.toPrettyYaml(this);
}

public static int compareBuild(StackGresExtensionMetadata left,
StackGresExtensionMetadata right) {
int compare = left.majorBuild.compareTo(right.majorBuild);
@Override
public int compareTo(StackGresExtensionMetadata o) {
int compare = build.compareTo(o.build);
if (compare == 0) {
compare = left.minorBuild.compareTo(right.minorBuild);
String[] versionParts = version.getVersion().split("\\.");
String[] otherVersionParts = o.version.getVersion().split("\\.");
for (int i = 0; i < versionParts.length && i < otherVersionParts.length; i++) {
int versionPartNumber = getNumberFromVersionPart(versionParts[i]);
int otherVersionPartNumber = getNumberFromVersionPart(otherVersionParts[i]);
if (versionPartNumber != otherVersionPartNumber) {
return versionPartNumber - otherVersionPartNumber;
}
}
}
return compare;
}

private int getNumberFromVersionPart(String versionPart) {
try {
return Integer.parseInt(versionPart);
} catch (NumberFormatException ex) {
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2019 OnGres, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package io.stackgres.common.extension;

import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.stackgres.common.StackGresUtil;

public class StackGresExtensionMetadataBuild
implements Comparable<StackGresExtensionMetadataBuild> {

private static final Pattern BUILD_PATTERN = Pattern.compile(
"^(?<major>[0-9]+)\\.(?<minor>[0-9]+)(?:-dev)?$");

private final Integer majorBuild;
private final Integer minorBuild;

public StackGresExtensionMetadataBuild(String build) {
Optional<Matcher> matcher = Optional.ofNullable(build)
.map(BUILD_PATTERN::matcher)
.filter(Matcher::find);
this.majorBuild = matcher
.map(m -> m.group("major"))
.map(Integer::parseInt)
.orElse(0);
this.minorBuild = matcher
.map(m -> m.group("minor"))
.map(Integer::parseInt)
.orElse(0);
}

public int getMajorBuild() {
return majorBuild;
}

public int getMinorBuild() {
return minorBuild;
}

@Override
public int hashCode() {
return Objects.hash(majorBuild, minorBuild);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof StackGresExtensionMetadataBuild)) {
return false;
}
StackGresExtensionMetadataBuild other = (StackGresExtensionMetadataBuild) obj;
return Objects.equals(majorBuild, other.majorBuild)
&& Objects.equals(minorBuild, other.minorBuild);
}

@Override
public String toString() {
return StackGresUtil.toPrettyYaml(this);
}

@Override
public int compareTo(StackGresExtensionMetadataBuild o) {
int compare = majorBuild.compareTo(o.majorBuild);
if (compare == 0) {
compare = minorBuild.compareTo(o.minorBuild);
}
return compare;
}

}
34 changes: 14 additions & 20 deletions stackgres-k8s/src/common/src/main/resources/crds/SGConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,20 @@ spec:
type: object
x-kubernetes-preserve-unknown-fields: true
description: Operator Installation Jobs affinity. See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.27/#affinity-v1-core
serviceAccount:
type: object
description: Section to configure Jobs ServiceAccount
properties:
annotations:
type: object
x-kubernetes-preserve-unknown-fields: true
description: Jobs ServiceAccount annotations
repoCredentials:
type: array
description: Repositories credentials Secret names
items:
type: string
description: Repository credentials Secret name
deploy:
type: object
description: Section to configure deployment aspects.
Expand Down Expand Up @@ -380,26 +394,6 @@ spec:
description: |
The duration in days of the generated RSA key pair for the Web Console / REST API after which it will expire and be regenerated.
If not specified it will be set to 730 (2 years) by default.
key:
type: string
description: |
The private RSA key used to create the Operator Webhooks certificate issued by the
Kubernetes cluster CA.
crt:
type: string
description: The Operator Webhooks certificate issued by Kubernetes cluster CA.
jwtRsaKey:
type: string
description: The private RSA key used to generate JWTs used in REST API authentication.
jwtRsaPub:
type: string
description: The public RSA key used to verify JWTs used in REST API authentication.
webKey:
type: string
description: The private RSA key used to create the Web Console / REST API certificate
webCrt:
type: string
description: The Web Console / REST API certificate
certManager:
type: object
description: Section to configure cert-manager integration to generate Operator certificates
Expand Down

0 comments on commit 7cab40d

Please sign in to comment.