Skip to content

Commit

Permalink
feat(installer): support upgrade through installer (#939)
Browse files Browse the repository at this point in the history
* feat(installer): support upgrade through installer

* fix(installer): fix installer registry nil pointer

* feat(platform): remove save third party registry

* feat(platform): load tke data before upgrade

* feat(platform): move load func to installer
  • Loading branch information
Leo Ryu committed Dec 1, 2020
1 parent 719cb2a commit 10e2ac1
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 65 deletions.
2 changes: 1 addition & 1 deletion build/docker/tools/tke-installer/init_installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tailNum=$((tailNum +1))
tail -n +${tailNum} "${me}" >package.tgz || die
tar -zxf package.tgz || die

./install.sh || die
./install.sh $@ || die
cd "${cwd}" || die
exit 0

Expand Down
2 changes: 1 addition & 1 deletion build/docker/tools/tke-installer/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function clean_old_data() {
function start_installer() {
echo "Step.5 start tke-installer [doing]"

docker run $OPTIONS "tkestack/tke-installer-${ARCH}:$VERSION"
docker run $OPTIONS "tkestack/tke-installer-${ARCH}:$VERSION" $@

echo "Step.5 start tke-installer [ok]"
}
Expand Down
12 changes: 12 additions & 0 deletions cmd/tke-installer/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ type Config struct {
Force bool
SyncProjectsWithNamespaces bool
Replicas int
Upgrade bool
Kubeconfig string
RegistryUsername string
RegistryPassword string
RegistryDomain string
RegistryNamespace string
}

// CreateConfigFromOptions creates a running configuration instance based
Expand All @@ -48,5 +54,11 @@ func CreateConfigFromOptions(serverName string, opts *options.Options) (*Config,
Force: *opts.Force,
SyncProjectsWithNamespaces: *opts.SyncProjectsWithNamespaces,
Replicas: *opts.Replicas,
Upgrade: *opts.Upgrade,
Kubeconfig: *opts.Kubeconfig,
RegistryUsername: *opts.RegistryUsername,
RegistryPassword: *opts.RegistryPassword,
RegistryDomain: *opts.RegistryDomain,
RegistryNamespace: *opts.RegistryNamespace,
}, nil
}
55 changes: 40 additions & 15 deletions cmd/tke-installer/app/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,8 @@ func New(config *config.Config) *TKE {
c.docker.Stderr = c.log

if !config.Force {
data, err := ioutil.ReadFile(constants.ClusterFile)
err = c.loadTKEData()
if err == nil {
log.Infof("read %q success", constants.ClusterFile)
err = json.Unmarshal(data, c)
if err != nil {
log.Warnf("load tke data error:%s", err)
}
log.Infof("load tke data success")
c.isFromRestore = true
c.progress.Status = types.StatusDoing
}
Expand All @@ -157,6 +151,20 @@ func New(config *config.Config) *TKE {
return c
}

func (t *TKE) loadTKEData() error {
data, err := ioutil.ReadFile(constants.ClusterFile)
if err == nil {
t.log.Infof("read %q success", constants.ClusterFile)
err = json.Unmarshal(data, t)
if err != nil {
t.log.Infof("load tke data error:%s", err)
} else {
log.Infof("load tke data success")
}
}
return err
}

func (t *TKE) initSteps() {
t.steps = append(t.steps, []types.Handler{
{
Expand Down Expand Up @@ -208,8 +216,8 @@ func (t *TKE) initSteps() {
Func: t.createGlobalCluster,
},
{
Name: "Patch k8s version in cluster info",
Func: t.patchK8sValidVersions,
Name: "Patch platform versions in cluster info",
Func: t.patchPlatformVersion,
},
{
Name: "Write kubeconfig",
Expand Down Expand Up @@ -464,6 +472,15 @@ func (t *TKE) runWithUI() error {

restful.Filter(globalLogging)

if t.Config.Upgrade {
err := t.prepareForUpgrade(context.Background())
if err != nil {
return err
}
t.do()
return nil
}

if t.isFromRestore {
go t.do()
}
Expand Down Expand Up @@ -953,15 +970,22 @@ func (t *TKE) do() {
start := time.Now()
ctx := t.log.WithContext(context.Background())

containerregistry.Init(t.Para.Config.Registry.Domain(), t.Para.Config.Registry.Namespace())
t.initSteps()
var taskType string
if t.Config.Upgrade {
taskType = "upgrade"
t.upgradeSteps()
} else {
taskType = "install"
containerregistry.Init(t.Para.Config.Registry.Domain(), t.Para.Config.Registry.Namespace())
t.initSteps()
}

if t.Step == 0 {
t.log.Info("===>starting install task")
t.log.Infof("===>starting %s task", taskType)
t.progress.Status = types.StatusDoing
}

if t.runAfterClusterReady() {
if !t.Config.Upgrade && t.runAfterClusterReady() {
t.initDataForDeployTKE()
}

Expand Down Expand Up @@ -1020,7 +1044,7 @@ func (t *TKE) do() {
}
t.progress.Servers = append(t.progress.Servers, t.servers...)

t.log.Infof("===>install task [Sucesss] [%fs]", time.Since(start).Seconds())
t.log.Infof("===>%s task [Sucesss] [%fs]", taskType, time.Since(start).Seconds())
}

func (t *TKE) runAfterClusterReady() bool {
Expand Down Expand Up @@ -2425,14 +2449,15 @@ func (t *TKE) writeKubeconfig(ctx context.Context) error {
return ioutil.WriteFile("/root/.kube/config", data, 0644)
}

func (t *TKE) patchK8sValidVersions(ctx context.Context) error {
func (t *TKE) patchPlatformVersion(ctx context.Context) error {
versionsByte, err := json.Marshal(spec.K8sValidVersions)
if err != nil {
return err
}
patchData := map[string]interface{}{
"data": map[string]interface{}{
"k8sValidVersions": string(versionsByte),
"tkeVersion": spec.TKEVersion,
},
}
patchByte, err := json.Marshal(patchData)
Expand Down
20 changes: 20 additions & 0 deletions cmd/tke-installer/app/installer/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type OIDCAuth struct {
type Registry struct {
TKERegistry *TKERegistry `json:"tke,omitempty"`
ThirdPartyRegistry *ThirdPartyRegistry `json:"thirdParty,omitempty"`
UserInputRegistry UserInputRegistry `json:"userInputRegistry,omitempty"`
}

type Audit struct {
Expand All @@ -87,27 +88,39 @@ type ElasticSearch struct {
}

func (r *Registry) Domain() string {
if r.UserInputRegistry.Domain != "" {
return r.UserInputRegistry.Domain
}
if r.ThirdPartyRegistry != nil { // first use third party when both set
return r.ThirdPartyRegistry.Domain
}
return r.TKERegistry.Domain
}

func (r *Registry) Namespace() string {
if r.UserInputRegistry.Namespace != "" {
return r.UserInputRegistry.Namespace
}
if r.ThirdPartyRegistry != nil {
return r.ThirdPartyRegistry.Namespace
}
return r.TKERegistry.Namespace
}

func (r *Registry) Username() string {
if r.UserInputRegistry.Username != "" {
return r.UserInputRegistry.Username
}
if r.ThirdPartyRegistry != nil {
return r.ThirdPartyRegistry.Username
}
return r.TKERegistry.Username
}

func (r *Registry) Password() []byte {
if len(r.UserInputRegistry.Password) != 0 {
return r.UserInputRegistry.Password
}
if r.ThirdPartyRegistry != nil {
return r.ThirdPartyRegistry.Password
}
Expand Down Expand Up @@ -138,6 +151,13 @@ type ThirdPartyRegistry struct {
Password []byte `json:"password"`
}

type UserInputRegistry struct {
Domain string `json:"domain"`
Namespace string `json:"namespace"`
Username string `json:"username"`
Password []byte `json:"password"`
}

type Business struct {
}

Expand Down

0 comments on commit 10e2ac1

Please sign in to comment.