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

Refactor to set pullsecret and clusterID #869

Merged
merged 2 commits into from
Dec 12, 2019
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
80 changes: 80 additions & 0 deletions pkg/crc/cluster/cluster.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package cluster

import (
"encoding/base64"
"fmt"
"strconv"
"strings"
"time"

"github.com/code-ready/crc/pkg/crc/errors"
"github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/crc/pkg/crc/oc"
"github.com/code-ready/crc/pkg/crc/ssh"
"github.com/pborman/uuid"
)

func WaitForSsh(sshRunner *ssh.SSHRunner) error {
Expand Down Expand Up @@ -77,3 +81,79 @@ func GetRootPartitionUsage(sshRunner *ssh.SSHRunner) (int64, int64, error) {
}
return diskSize, diskUsage, nil
}

func AddPullSecret(sshRunner *ssh.SSHRunner, oc oc.OcConfig, pullSec string) error {
if err := addPullSecretToInstanceDisk(sshRunner, pullSec); err != nil {
return err
}

base64OfPullSec := base64.StdEncoding.EncodeToString([]byte(pullSec))
cmdArgs := []string{"patch", "secret", "pull-secret", "-p",
cfergeau marked this conversation as resolved.
Show resolved Hide resolved
fmt.Sprintf(`{"data":{".dockerconfigjson":"%s"}}`, base64OfPullSec),
"-n", "openshift-config", "--type", "merge"}

if err := waitForOpenshiftAPIServer(oc); err != nil {
return err
}
_, stderr, err := oc.RunOcCommand(cmdArgs...)
if err != nil {
return fmt.Errorf("Failed to add Pull secret %v: %s", err, stderr)
}
return nil
}

func UpdateClusterID(oc oc.OcConfig) error {
clusterID := uuid.New()
cmdArgs := []string{"patch", "clusterversion", "version", "-p",
fmt.Sprintf(`{"spec":{"clusterID":"%s"}}`, clusterID), "--type", "merge"}

if err := waitForOpenshiftAPIServer(oc); err != nil {
return err
}
_, stderr, err := oc.RunOcCommand(cmdArgs...)
if err != nil {
return fmt.Errorf("Failed to update cluster ID %v: %s", err, stderr)
}

return nil
}

func StopAndRemovePodsInVM(sshRunner *ssh.SSHRunner) error {
// This command make sure we stop the kubelet and clean up the pods
// We also providing a 2 seconds sleep so that stopped pods get settled and
// ready for removal. Without this 2 seconds time sometime it happens some of
// the pods are not completely stopped and when remove happens it will throw
// an error like below.
// remove /var/run/containers/storage/overlay-containers/97e5858e610afc9f71d145b1a7bd5ad930e537ccae79969ae256636f7fb7e77c/userdata/shm: device or resource busy
stopAndRemovePodsCmd := `bash -c 'sudo crictl stopp $(sudo crictl pods -q) && sudo crictl rmp $(sudo crictl pods -q)'`
stopAndRemovePods := func() error {
output, err := sshRunner.Run(stopAndRemovePodsCmd)
logging.Debugf("Output of %s: %s", stopAndRemovePodsCmd, output)
if err != nil {
return &errors.RetriableError{Err: err}
}
return nil
}

return errors.RetryAfter(2, stopAndRemovePods, 2*time.Second)
}

func addPullSecretToInstanceDisk(sshRunner *ssh.SSHRunner, pullSec string) error {
_, err := sshRunner.RunPrivate(fmt.Sprintf("cat <<EOF | sudo tee /var/lib/kubelet/config.json\n%s\nEOF", pullSec))
if err != nil {
return err
}
return nil
}

func waitForOpenshiftAPIServer(oc oc.OcConfig) error {
waitForApiServer := func() error {
_, stderr, err := oc.RunOcCommand("get", "pods")
if err != nil {
logging.Debug(stderr)
return &errors.RetriableError{Err: err}
}
return nil
}
return errors.RetryAfter(80, waitForApiServer, time.Second)
cfergeau marked this conversation as resolved.
Show resolved Hide resolved
}
46 changes: 39 additions & 7 deletions pkg/crc/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/code-ready/crc/pkg/crc/errors"
"github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/crc/pkg/crc/network"
"github.com/code-ready/crc/pkg/crc/pullsecret"
crcssh "github.com/code-ready/crc/pkg/crc/ssh"
"github.com/code-ready/crc/pkg/crc/systemd"
crcos "github.com/code-ready/crc/pkg/os"
Expand Down Expand Up @@ -314,6 +313,14 @@ func Start(startConfig StartConfig) (StartResult, error) {
logging.Warnf("Failed public DNS query from the cluster: %v : %s", err, queryOutput)
}

// Check DNS lookup from host to VM
logging.Info("Check DNS query from host ...")
if err := network.CheckCRCLocalDNSReachableFromHost(crcBundleMetadata.ClusterInfo.ClusterName,
crcBundleMetadata.ClusterInfo.BaseDomain, crcBundleMetadata.ClusterInfo.AppsDomain); err != nil {
result.Error = err.Error()
return *result, errors.Newf("Failed to query DNS from host: %v", err)
}

// Additional steps to perform after newly created VM is up
if !exists {
if err := updateSSHKeyPair(sshRunner); err != nil {
Expand Down Expand Up @@ -342,13 +349,11 @@ func Start(startConfig StartConfig) (StartResult, error) {
}
}

ocConfig := oc.UseOCWithConfig(startConfig.Name)
if !exists {
// Update the user pull secret before kubelet start.
logging.Info("Adding user's pull secret and cluster ID ...")
kubeConfigFilePath := filepath.Join(constants.MachineInstanceDir, startConfig.Name, "kubeconfig")
if err := pullsecret.AddPullSecretAndClusterID(sshRunner, pullSecret, kubeConfigFilePath); err != nil {
if err := configureCluster(ocConfig, sshRunner, pullSecret); err != nil {
result.Error = err.Error()
return *result, errors.Newf("Failed to update user pull secret or cluster ID: %v", err)
return *result, errors.Newf("Error Setting cluster config: %s", err)
}
}

Expand All @@ -367,7 +372,6 @@ func Start(startConfig StartConfig) (StartResult, error) {
time.Sleep(time.Minute * 3)

// Approve the node certificate.
ocConfig := oc.UseOCWithConfig(startConfig.Name)
if err := ocConfig.ApproveNodeCSR(); err != nil {
result.Error = err.Error()
return *result, errors.Newf("Error approving the node csr %v", err)
Expand Down Expand Up @@ -675,3 +679,31 @@ func updateSSHKeyPair(sshRunner *crcssh.SSHRunner) error {

return err
}

func configureCluster(ocConfig oc.OcConfig, sshRunner *crcssh.SSHRunner, pullSecret string) (rerr error) {
sd := systemd.NewInstanceSystemdCommander(sshRunner)
if _, err := sd.Start("kubelet"); err != nil {
return fmt.Errorf("Error starting kubelet: %s", err)
}

defer func() {
// Stop the kubelet service.
if _, err := sd.Stop("kubelet"); err != nil {
rerr = err
}
if err := cluster.StopAndRemovePodsInVM(sshRunner); err != nil {
rerr = err
}
rerr = nil
}()

logging.Info("Adding user's pull secret ...")
if err := cluster.AddPullSecret(sshRunner, ocConfig, pullSecret); err != nil {
return fmt.Errorf("Failed to update user pull secret or cluster ID: %v", err)
}
logging.Info("Updating cluster ID ...")
if err := cluster.UpdateClusterID(ocConfig); err != nil {
return fmt.Errorf("Failed to update cluster ID: %v", err)
}
return nil
}
15 changes: 15 additions & 0 deletions pkg/crc/network/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package network

import (
"fmt"
"github.com/code-ready/crc/pkg/crc/logging"
"net"
"net/url"

Expand Down Expand Up @@ -92,3 +93,17 @@ func UriStringForDisplay(uri string) (string, error) {
}
return uri, nil
}

func CheckCRCLocalDNSReachableFromHost(clusterName, domainName, appsDomainName string) error {
ip, err := net.LookupIP(fmt.Sprintf("api.%s.%s", clusterName, domainName))
if err != nil {
return err
}
logging.Debugf("api.%s.%s resolved to %s", clusterName, domainName, ip)
ip, err = net.LookupIP(fmt.Sprintf("foo.%s", appsDomainName))
if err != nil {
return err
}
logging.Debugf("foo.%s resolved to %s", appsDomainName, ip)
return nil
}
158 changes: 0 additions & 158 deletions pkg/crc/pullsecret/pullsecret.go

This file was deleted.