Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with init-container when persistence is enabled with a Pod securityContext #636

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix issue with init-container when persistence is enabled
  • Loading branch information
thegridman committed Nov 29, 2023
commit 5b5877fa6523cdaa321b041a854e22ba05e8a8e0
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,21 @@ void initCohCtl() {
String defaultProtocol = provider == null || provider.isEmpty() ? "http" : "https";
String protocol = Config.getProperty("coherence.operator.cli.protocol", defaultProtocol);
String home = System.getProperty("user.home");
File fileHome = new File(home);
String connectionType = "http";

if (!fileHome.exists() || !fileHome.isDirectory()) {
LOGGER.info("CoherenceOperator: user home \"" + home
+ "\" does not exist, creating default cohctl config at /coherence-operator/utils");
home = "/coherence-operator/utils";
fileHome = new File(home);
}

if (!fileHome.exists() || !fileHome.isDirectory()) {
LOGGER.error("CoherenceOperator: Cannot create cohctl config, directory " + home + " does not exist");
return;
}

File cohctlHome = new File(home + File.separator + ".cohctl");
File configFile = new File(cohctlHome, "cohctl.yaml");

Expand Down Expand Up @@ -320,7 +333,7 @@ void initCohCtl() {
}
}
catch (Exception e) {
LOGGER.error(e, "Coherence Operator: Failed to create default cohctl config");
LOGGER.error("Coherence Operator: Failed to create default cohctl config. " + e.getMessage());
}
}
}
28 changes: 5 additions & 23 deletions pkg/runner/cmd_initialise.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ func initialiseWithEnv(cmd *cobra.Command, getEnv EnvFunction) (bool, error) {
persistenceTrashDir := persistenceDir + pathSep + "trash"
persistenceSnapshotsDir := persistenceDir + pathSep + "snapshots"

snapshotDir, err := flagSet.GetString(ArgSnapshotsDir)
if err != nil {
return false, err
clusterName := getEnv(v1.EnvVarCohClusterName)
if clusterName != "" {
persistenceActiveDir = persistenceActiveDir + pathSep + clusterName
persistenceTrashDir = persistenceTrashDir + pathSep + clusterName
persistenceSnapshotsDir = persistenceSnapshotsDir + pathSep + clusterName
}

fmt.Println("Starting container initialisation")
Expand Down Expand Up @@ -168,32 +170,12 @@ func initialiseWithEnv(cmd *cobra.Command, getEnv EnvFunction) (bool, error) {
dirNames = append(dirNames, persistenceActiveDir, persistenceTrashDir, persistenceSnapshotsDir)
}

_, err = os.Stat(snapshotDir)
if err == nil {
// if "/snapshot" exists then we'll create the cluster snapshot directory
clusterName := getEnv(v1.EnvVarCohClusterName)
if clusterName != "" {
snapshotClusterDir := pathSep + "snapshot" + pathSep + clusterName
dirNames = append(dirNames, snapshotClusterDir)
}
}

for _, dirName := range dirNames {
fmt.Printf("Creating directory %s\n", dirName)
err = os.MkdirAll(dirName, os.ModePerm)
if err != nil {
return false, err
}
info, err := os.Stat(dirName)
if err != nil {
return false, err
}
if info.Mode().Perm() != os.ModePerm {
err = os.Chmod(dirName, os.ModePerm)
if err != nil {
return false, err
}
}
}

fmt.Println("Finished container initialisation")
Expand Down
46 changes: 46 additions & 0 deletions test/e2e/remote/persistence-active-snapshot-security.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: coherence.oracle.com/v1
kind: Coherence
metadata:
name: snapshot-cluster
spec:
role: one
replicas: 1
image: ${TEST_APPLICATION_IMAGE}
imagePullPolicy: ${TEST_IMAGE_PULL_POLICY}
securityContext:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
fsGroupChangePolicy: "OnRootMismatch"
readinessProbe:
initialDelaySeconds: 10
periodSeconds: 10
coherence:
cacheConfig: test-cache-config.xml
management:
enabled: true
persistence:
mode: active
persistentVolumeClaim:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
snapshots:
persistentVolumeClaim:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
jvm:
args:
- "-Dcoherence.k8s.operator.health.logs=true"
application:
main: com.oracle.coherence.k8s.testing.RestServer
ports:
- name: management
port: 30000
- name: rest
port: 8080
10 changes: 10 additions & 0 deletions test/e2e/remote/persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ func TestSnapshotWithActivePersistence(t *testing.T) {
assertPersistence("persistence-active-snapshot.yaml", "snapshot-volume", true, true, true, t)
}

// Deploy a Coherence resource with both persistence and snapshot configured and a securityContext so
// that the container is not running as the root user.
// Persistence will be active, a PVC will be created for the StatefulSet. Put data in a cache, take a snapshot,
// delete the deployment, re-deploy the deployment, recover the snapshot, assert that the data is recovered.
func TestSnapshotWithActivePersistenceWithSecurityContext(t *testing.T) {
// Make sure we defer clean-up when we're done!!
testContext.CleanupAfterTest(t)
assertPersistence("persistence-active-snapshot-security.yaml", "snapshot-volume", true, true, true, t)
}

func assertPersistence(yamlFile, pVolName string, isSnapshot, isClearCanary, isRestart bool, t *testing.T) {
g := NewGomegaWithT(t)
ns := helper.GetTestNamespace()
Expand Down