Skip to content

Commit a7dfae7

Browse files
committed
Copy dependencies of systemd units
copy pull secret file into the VM this removes code adding the pull-secret to the cluster using `oc`, instead it copies the pull secret file to /opt/crc/crc-pullsecret which is then used by a systemd service in the bundle to add the pull secret to the cluster for both the openshift and microshift presets Update cluster user passwords via systemd this copies the generated kubeadmin and developer user passwords to `/opt/crc/` which is then used by a systemd service and modifies the needed ocp resources Use systemd to add the root CA for API server access this removes the code patching the configmap admin-kubeconfig-client-ca to use the custom CA, instead it copies the generated CA to '/opt/crc/' which is then used by a systemd service to created the required secret and updates the configmap
1 parent aedf6d4 commit a7dfae7

File tree

4 files changed

+60
-80
lines changed

4 files changed

+60
-80
lines changed

pkg/crc/cluster/cluster.go

+3-41
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cluster
33
import (
44
"context"
55
"crypto/x509"
6-
"encoding/base64"
76
"encoding/json"
87
"fmt"
98
"math"
@@ -183,40 +182,6 @@ func EnsureSSHKeyPresentInTheCluster(ctx context.Context, ocConfig oc.Config, ss
183182
return nil
184183
}
185184

186-
func EnsurePullSecretPresentInTheCluster(ctx context.Context, ocConfig oc.Config, pullSec PullSecretLoader) error {
187-
if err := WaitForOpenshiftResource(ctx, ocConfig, "secret"); err != nil {
188-
return err
189-
}
190-
191-
stdout, stderr, err := ocConfig.RunOcCommandPrivate("get", "secret", "pull-secret", "-n", "openshift-config", "-o", `jsonpath="{['data']['\.dockerconfigjson']}"`)
192-
if err != nil {
193-
return fmt.Errorf("Failed to get pull secret %v: %s", err, stderr)
194-
}
195-
decoded, err := base64.StdEncoding.DecodeString(stdout)
196-
if err != nil {
197-
return err
198-
}
199-
if err := validation.ImagePullSecret(string(decoded)); err == nil {
200-
return nil
201-
}
202-
203-
logging.Info("Adding user's pull secret to the cluster...")
204-
content, err := pullSec.Value()
205-
if err != nil {
206-
return err
207-
}
208-
base64OfPullSec := base64.StdEncoding.EncodeToString([]byte(content))
209-
cmdArgs := []string{"patch", "secret", "pull-secret", "-p",
210-
fmt.Sprintf(`'{"data":{".dockerconfigjson":"%s"}}'`, base64OfPullSec),
211-
"-n", "openshift-config", "--type", "merge"}
212-
213-
_, stderr, err = ocConfig.RunOcCommandPrivate(cmdArgs...)
214-
if err != nil {
215-
return fmt.Errorf("Failed to add Pull secret %v: %s", err, stderr)
216-
}
217-
return nil
218-
}
219-
220185
func EnsureGeneratedClientCAPresentInTheCluster(ctx context.Context, ocConfig oc.Config, sshRunner *ssh.Runner, selfSignedCACert *x509.Certificate, adminCert string) error {
221186
selfSignedCAPem := crctls.CertToPem(selfSignedCACert)
222187
if err := WaitForOpenshiftResource(ctx, ocConfig, "configmaps"); err != nil {
@@ -236,13 +201,10 @@ func EnsureGeneratedClientCAPresentInTheCluster(ctx context.Context, ocConfig oc
236201
}
237202

238203
logging.Info("Updating root CA cert to admin-kubeconfig-client-ca configmap...")
239-
jsonPath := fmt.Sprintf(`'{"data": {"ca-bundle.crt": %q}}'`, selfSignedCAPem)
240-
cmdArgs := []string{"patch", "configmap", "admin-kubeconfig-client-ca",
241-
"-n", "openshift-config", "--patch", jsonPath}
242-
_, stderr, err = ocConfig.RunOcCommand(cmdArgs...)
243-
if err != nil {
244-
return fmt.Errorf("Failed to patch admin-kubeconfig-client-ca config map with new CA` %v: %s", err, stderr)
204+
if err := sshRunner.CopyDataPrivileged(selfSignedCAPem, "/opt/crc/custom-ca.crt", 0644); err != nil {
205+
return fmt.Errorf("Failed to copy generated CA file to VM: %v", err)
245206
}
207+
246208
if err := sshRunner.CopyFile(constants.KubeconfigFilePath, ocConfig.KubeconfigPath, 0644); err != nil {
247209
return fmt.Errorf("Failed to copy generated kubeconfig file to VM: %v", err)
248210
}

pkg/crc/cluster/kubeadmin_password.go

+4-29
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
"github.com/crc-org/crc/v2/pkg/crc/constants"
1515
"github.com/crc-org/crc/v2/pkg/crc/logging"
16-
"github.com/crc-org/crc/v2/pkg/crc/oc"
16+
"github.com/crc-org/crc/v2/pkg/crc/ssh"
1717
"golang.org/x/crypto/bcrypt"
1818
)
1919

@@ -29,7 +29,7 @@ func GenerateKubeAdminUserPassword() error {
2929
}
3030

3131
// UpdateKubeAdminUserPassword updates the htpasswd secret
32-
func UpdateKubeAdminUserPassword(ctx context.Context, ocConfig oc.Config, newPassword string) error {
32+
func UpdateKubeAdminUserPassword(ctx context.Context, sshRunner *ssh.Runner, newPassword string) error {
3333
if newPassword != "" {
3434
logging.Infof("Overriding password for kubeadmin user")
3535
if err := os.WriteFile(constants.GetKubeAdminPasswordPath(), []byte(strings.TrimSpace(newPassword)), 0600); err != nil {
@@ -41,39 +41,14 @@ func UpdateKubeAdminUserPassword(ctx context.Context, ocConfig oc.Config, newPas
4141
if err != nil {
4242
return fmt.Errorf("Cannot read the kubeadmin user password from file: %w", err)
4343
}
44-
credentials := map[string]string{
45-
"developer": "developer",
46-
"kubeadmin": kubeAdminPassword,
47-
}
4844

49-
if err := WaitForOpenshiftResource(ctx, ocConfig, "secret"); err != nil {
45+
if err := sshRunner.CopyDataPrivileged([]byte(kubeAdminPassword), "/opt/crc/pass_kubeadmin", 0600); err != nil {
5046
return err
5147
}
5248

53-
given, stderr, err := ocConfig.RunOcCommandPrivate("get", "secret", "htpass-secret", "-n", "openshift-config", "-o", `jsonpath="{.data.htpasswd}"`)
54-
if err != nil {
55-
return fmt.Errorf("%s:%v", stderr, err)
56-
}
57-
ok, externals, err := compareHtpasswd(given, credentials)
58-
if err != nil {
49+
if err := sshRunner.CopyDataPrivileged([]byte("developer"), "/opt/crc/pass_developer", 0600); err != nil {
5950
return err
6051
}
61-
if ok {
62-
return nil
63-
}
64-
65-
logging.Infof("Changing the password for the kubeadmin user")
66-
expected, err := getHtpasswd(credentials, externals)
67-
if err != nil {
68-
return err
69-
}
70-
cmdArgs := []string{"patch", "secret", "htpass-secret", "-p",
71-
fmt.Sprintf(`'{"data":{"htpasswd":"%s"}}'`, expected),
72-
"-n", "openshift-config", "--type", "merge"}
73-
_, stderr, err = ocConfig.RunOcCommandPrivate(cmdArgs...)
74-
if err != nil {
75-
return fmt.Errorf("Failed to update kubeadmin password %v: %s", err, stderr)
76-
}
7752
return nil
7853
}
7954

pkg/crc/machine/start.go

+41-10
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,10 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
277277
if err := validation.BundleMismatchWithPresetMetadata(startConfig.Preset, crcBundleMetadata); err != nil {
278278
return nil, err
279279
}
280+
var firstBoot bool
280281

281282
if !exists {
283+
firstBoot = true
282284
telemetry.SetStartType(ctx, telemetry.CreationStartType)
283285

284286
// Ask early for pull secret if it hasn't been requested yet
@@ -315,6 +317,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
315317
return nil, errors.Wrap(err, "Error creating machine")
316318
}
317319
} else {
320+
firstBoot = false
318321
telemetry.SetStartType(ctx, telemetry.StartStartType)
319322
}
320323

@@ -426,6 +429,35 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
426429
}
427430
}
428431

432+
// setup the env file for units to detect the network-mode either user or systemd
433+
// refactor into a helper `setSystemdEnvFileValues`
434+
if client.useVSock() {
435+
envs := "CRC_NETWORK_MODE_USER=1" + "\n" +
436+
"CRC_DEBUG_TEST=1" + "\n"
437+
438+
if err := sshRunner.CopyDataPrivileged([]byte(envs), "/etc/systemd/system/crc-env", 0644); err != nil {
439+
return nil, errors.Wrap(err, "Unable to create the env file for CRC")
440+
}
441+
} else {
442+
envs := "CRC_NETWORK_MODE_USER=0" + "\n" +
443+
"CRC_DEBUG_TEST=1" + "\n"
444+
445+
if err := sshRunner.CopyDataPrivileged([]byte(envs), "/etc/systemd/system/crc-env", 0644); err != nil {
446+
return nil, errors.Wrap(err, "Unable to create the env file for CRC")
447+
}
448+
}
449+
450+
// copy the pull secret into /opt/crc/pull-secret in the instance
451+
if firstBoot {
452+
pullSecret, err := startConfig.PullSecret.Value()
453+
if err != nil {
454+
return nil, err
455+
}
456+
if err := sshRunner.CopyDataPrivileged([]byte(pullSecret), "/opt/crc/pull-secret", 0600); err != nil {
457+
return nil, errors.Wrap(err, "Unable to send pull-secret to instance")
458+
}
459+
}
460+
429461
// Add nameserver to VM if provided by User
430462
if startConfig.NameServer != "" {
431463
if err = addNameServerToInstance(sshRunner, startConfig.NameServer); err != nil {
@@ -511,6 +543,11 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
511543
}, nil
512544
}
513545

546+
// Send the kubeadmin and developer new passwords to the VM
547+
if err := cluster.UpdateKubeAdminUserPassword(ctx, sshRunner, startConfig.KubeAdminPassword); err != nil {
548+
return nil, errors.Wrap(err, "Failed to update kubeadmin user password")
549+
}
550+
514551
// Check the certs validity inside the vm
515552
logging.Info("Verifying validity of the kubelet certificates...")
516553
certsExpired, err := cluster.CheckCertsValidity(sshRunner)
@@ -543,10 +580,6 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
543580
return nil, err
544581
}
545582

546-
if err := cluster.EnsurePullSecretPresentInTheCluster(ctx, ocConfig, startConfig.PullSecret); err != nil {
547-
return nil, errors.Wrap(err, "Failed to update cluster pull secret")
548-
}
549-
550583
if err := cluster.EnsureSSHKeyPresentInTheCluster(ctx, ocConfig, constants.GetPublicKeyPath()); err != nil {
551584
return nil, errors.Wrap(err, "Failed to update ssh public key to machine config")
552585
}
@@ -555,19 +588,17 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
555588
return nil, errors.Wrap(err, "Failed to update pull secret on the disk")
556589
}
557590

558-
if err := cluster.UpdateKubeAdminUserPassword(ctx, ocConfig, startConfig.KubeAdminPassword); err != nil {
559-
return nil, errors.Wrap(err, "Failed to update kubeadmin user password")
560-
}
561-
562591
if client.monitoringEnabled() {
563592
logging.Info("Enabling cluster monitoring operator...")
564593
if err := cluster.StartMonitoring(ocConfig); err != nil {
565594
return nil, errors.Wrap(err, "Cannot start monitoring stack")
566595
}
567596
}
568597

569-
if err := updateKubeconfig(ctx, ocConfig, sshRunner, vm.bundle.GetKubeConfigPath()); err != nil {
570-
return nil, errors.Wrap(err, "Failed to update kubeconfig file")
598+
if firstBoot {
599+
if err := updateKubeconfig(ctx, ocConfig, sshRunner, vm.bundle.GetKubeConfigPath()); err != nil {
600+
return nil, errors.Wrap(err, "Failed to update kubeconfig file")
601+
}
571602
}
572603

573604
logging.Infof("Starting %s instance... [waiting for the cluster to stabilize]", startConfig.Preset)

release-info.json-e

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": {
3+
"crcVersion": "2.45.0",
4+
"gitSha": "7d6313",
5+
"openshiftVersion": "@OPENSHIFT_VERSION@"
6+
},
7+
"links": {
8+
"linux": "https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/2.45.0/crc-linux-amd64.tar.xz",
9+
"darwin": "https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/2.45.0/crc-macos-installer.pkg",
10+
"windows": "https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/2.45.0/crc-windows-installer.zip"
11+
}
12+
}

0 commit comments

Comments
 (0)