Skip to content

Commit 44f0656

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 386aa3a commit 44f0656

File tree

3 files changed

+21
-78
lines changed

3 files changed

+21
-78
lines changed

pkg/crc/cluster/cluster.go

Lines changed: 3 additions & 41 deletions
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"
@@ -179,40 +178,6 @@ func EnsureSSHKeyPresentInTheCluster(ctx context.Context, ocConfig oc.Config, ss
179178
return nil
180179
}
181180

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

234199
logging.Info("Updating root CA cert to admin-kubeconfig-client-ca configmap...")
235-
jsonPath := fmt.Sprintf(`'{"data": {"ca-bundle.crt": %q}}'`, selfSignedCAPem)
236-
cmdArgs := []string{"patch", "configmap", "admin-kubeconfig-client-ca",
237-
"-n", "openshift-config", "--patch", jsonPath}
238-
_, stderr, err = ocConfig.RunOcCommand(cmdArgs...)
239-
if err != nil {
240-
return fmt.Errorf("Failed to patch admin-kubeconfig-client-ca config map with new CA` %v: %s", err, stderr)
200+
if err := sshRunner.CopyDataPrivileged(selfSignedCAPem, "/opt/crc/custom-ca.crt", 0644); err != nil {
201+
return fmt.Errorf("Failed to copy generated CA file to VM: %v", err)
241202
}
203+
242204
if err := sshRunner.CopyFile(constants.KubeconfigFilePath, ocConfig.KubeconfigPath, 0644); err != nil {
243205
return fmt.Errorf("Failed to copy generated kubeconfig file to VM: %v", err)
244206
}

pkg/crc/cluster/kubeadmin_password.go

Lines changed: 4 additions & 29 deletions
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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,15 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
424424
}
425425
}
426426

427+
// copy the pull secret into /opt/crc/pull-secret in the instance
428+
pullSecret, err := startConfig.PullSecret.Value()
429+
if err != nil {
430+
return nil, err
431+
}
432+
if err := sshRunner.CopyDataPrivileged([]byte(pullSecret), "/opt/crc/pull-secret", 0600); err != nil {
433+
return nil, errors.Wrap(err, "Unable to send pull-secret to instance")
434+
}
435+
427436
// Add nameserver to VM if provided by User
428437
if startConfig.NameServer != "" {
429438
if err = addNameServerToInstance(sshRunner, startConfig.NameServer); err != nil {
@@ -509,6 +518,11 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
509518
}, nil
510519
}
511520

521+
// Send the kubeadmin and developer new passwords to the VM
522+
if err := cluster.UpdateKubeAdminUserPassword(ctx, sshRunner, startConfig.KubeAdminPassword); err != nil {
523+
return nil, errors.Wrap(err, "Failed to update kubeadmin user password")
524+
}
525+
512526
// Check the certs validity inside the vm
513527
logging.Info("Verifying validity of the kubelet certificates...")
514528
certsExpired, err := cluster.CheckCertsValidity(sshRunner)
@@ -541,10 +555,6 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
541555
return nil, err
542556
}
543557

544-
if err := cluster.EnsurePullSecretPresentInTheCluster(ctx, ocConfig, startConfig.PullSecret); err != nil {
545-
return nil, errors.Wrap(err, "Failed to update cluster pull secret")
546-
}
547-
548558
if err := cluster.EnsureSSHKeyPresentInTheCluster(ctx, ocConfig, constants.GetPublicKeyPath()); err != nil {
549559
return nil, errors.Wrap(err, "Failed to update ssh public key to machine config")
550560
}
@@ -553,10 +563,6 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
553563
return nil, errors.Wrap(err, "Failed to update pull secret on the disk")
554564
}
555565

556-
if err := cluster.UpdateKubeAdminUserPassword(ctx, ocConfig, startConfig.KubeAdminPassword); err != nil {
557-
return nil, errors.Wrap(err, "Failed to update kubeadmin user password")
558-
}
559-
560566
if client.monitoringEnabled() {
561567
logging.Info("Enabling cluster monitoring operator...")
562568
if err := cluster.StartMonitoring(ocConfig); err != nil {

0 commit comments

Comments
 (0)