1
0
mirror of https://github.com/rancher/rke.git synced 2025-08-31 22:46:25 +00:00

init commit for refactor state

add init test

use rkeconfig for init

reconcile old state file
This commit is contained in:
galal-hussein
2018-11-01 01:11:57 +02:00
committed by Alena Prokharchyk
parent 631c0725f4
commit f48da22d8e
4 changed files with 171 additions and 1 deletions

View File

@@ -2,9 +2,13 @@ package cluster
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/rancher/rke/hosts"
@@ -18,8 +22,23 @@ import (
"gopkg.in/yaml.v2"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/cert"
)
const (
stateFileExt = ".rkestate"
)
type RKEFullState struct {
DesiredState RKEState `json:"desiredState,omitempty"`
CurrentState RKEState `json:"currentState,omitempty"`
}
type RKEState struct {
RancherKubernetesEngineConfig *v3.RancherKubernetesEngineConfig `json:"rkeConfig,omitempty"`
CertificatesBundle map[string]v3.CertificatePKI `json:"certificatesBundle,omitempty"`
}
func (c *Cluster) SaveClusterState(ctx context.Context, rkeConfig *v3.RancherKubernetesEngineConfig) error {
if len(c.ControlPlaneHosts) > 0 {
// Reinitialize kubernetes Client
@@ -249,3 +268,86 @@ func GetK8sVersion(localConfigPath string, k8sWrapTransport k8s.WrapTransport) (
}
return fmt.Sprintf("%#v", *serverVersion), nil
}
func GenerateDesiredState(ctx context.Context, rkeConfig *v3.RancherKubernetesEngineConfig, rkeFullState *RKEFullState) (RKEState, error) {
var desiredState RKEState
if rkeFullState.DesiredState.CertificatesBundle == nil {
// Get the certificate Bundle
certBundle, err := pki.GenerateRKECerts(ctx, *rkeConfig, "", "")
if err != nil {
return desiredState, fmt.Errorf("Failed to generate certificate bundle: %v", err)
}
// Convert rke certs to v3.certs
certificatesBundle := make(map[string]v3.CertificatePKI)
for name, certPKI := range certBundle {
certificatePEM := string(cert.EncodeCertPEM(certPKI.Certificate))
keyPEM := string(cert.EncodePrivateKeyPEM(certPKI.Key))
certificatesBundle[name] = v3.CertificatePKI{
Name: certPKI.Name,
Config: certPKI.Config,
Certificate: certificatePEM,
Key: keyPEM,
EnvName: certPKI.EnvName,
KeyEnvName: certPKI.KeyEnvName,
ConfigEnvName: certPKI.ConfigEnvName,
Path: certPKI.Path,
KeyPath: certPKI.KeyPath,
ConfigPath: certPKI.ConfigPath,
CommonName: certPKI.CommonName,
OUName: certPKI.OUName,
}
}
desiredState.CertificatesBundle = certificatesBundle
} else {
desiredState.CertificatesBundle = rkeFullState.DesiredState.CertificatesBundle
}
desiredState.RancherKubernetesEngineConfig = rkeConfig
return desiredState, nil
}
func (s *RKEFullState) WriteStateFile(ctx context.Context, statePath string) error {
stateFile, err := json.MarshalIndent(s, "", " ")
if err != nil {
return fmt.Errorf("Failed to Marshal state object: %v", err)
}
logrus.Debugf("Writing state file: %s", stateFile)
if err := ioutil.WriteFile(statePath, []byte(stateFile), 0640); err != nil {
return fmt.Errorf("Failed to write state file: %v", err)
}
log.Infof(ctx, "Successfully Deployed state file at [%s]", statePath)
return nil
}
func GetStateFilePath(configPath, configDir string) string {
baseDir := filepath.Dir(configPath)
if len(configDir) > 0 {
baseDir = filepath.Dir(configDir)
}
fileName := filepath.Base(configPath)
baseDir += "/"
fullPath := fmt.Sprintf("%s%s", baseDir, fileName)
trimmedName := strings.TrimSuffix(fullPath, filepath.Ext(fullPath))
return trimmedName + stateFileExt
}
func ReadStateFile(ctx context.Context, statePath string) (*RKEFullState, error) {
rkeFullState := &RKEFullState{}
fp, err := filepath.Abs(statePath)
if err != nil {
return rkeFullState, fmt.Errorf("failed to lookup current directory name: %v", err)
}
file, err := os.Open(fp)
if err != nil {
return rkeFullState, fmt.Errorf("Can not find RKE state file: %v", err)
}
defer file.Close()
buf, err := ioutil.ReadAll(file)
if err != nil {
return rkeFullState, fmt.Errorf("failed to read file: %v", err)
}
if err := json.Unmarshal(buf, rkeFullState); err != nil {
return rkeFullState, fmt.Errorf("failed to unmarshal the state file: %v", err)
}
return rkeFullState, nil
}