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

feature: k0s init, import some dependencies form k0sproject. #1619

Merged
merged 3 commits into from
Aug 14, 2022

Conversation

starComingup
Copy link
Member

@starComingup starComingup commented Aug 5, 2022

Signed-off-by: starComingup [email protected]

Describe what this PR does / why we need it

add k0s init step feature for sealer, it prepare k0sctl install env and image registry and so on.

Does this pull request fix one issue?

NONE, but is a step for k0s clusterRuntime.

Describe how you did it

NONE

Describe how to verify it

use unit test for check it work!

Todo list:

  • k.MergeConfigOnMaster0, // convert cluster file to k0s config
  • k.SSHCopyIDToHosts, // ssh-key-gen and ssh-copy-id to login hosts without passwd
  • k.GenerateCert, // generate Registry cert mainly
  • k.ApplyRegistryOnMaster0, // start a private registry for cluster

Note:
Why we don't need initMaster0 and GetKubectlConfig func like k8s init step?
In k0sctl design which lead the cluster creation, cluster init phase and node join phase integrate to k0sctl apply and it will reconcile by k0sctl itself, so to keep sealer now createProcess logic c.init and c.join. I would like to implement the k0sctl apply cmd in join function rather than init function. And after apply, and we can use k0sctl kubeconfig to create the config file for kubectl.

@starComingup
Copy link
Member Author

I use go mod tidy, but it seems so many dependencies imported.

@starComingup starComingup changed the title [WIP] feature: k0s init, import some dependencies form k0sproject. feature: k0s init, import some dependencies form k0sproject. Aug 8, 2022
func (k *Runtime) init() error {
pipeline := []func() error{
k.MergeConfigOnMaster0,
k.SSHKeyGenAndCopyIDToHosts,
Copy link
Member

@kakaZhou719 kakaZhou719 Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very familiar with k0s, I have a question, is this necessary to configure ssh key login?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we don't ssh-keygen id_rsa config: when k0sctl lead to install cluster, it will report:ERRO [ssh] 172.16.161.63:22: attempt 1 of 60.. failed to connect: stat /root/.ssh/id_rsa: no such file or directory
if we don't ssh-copy-id: k0sctl will report :failed to connect: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

pipeline := []func() error{
k.MergeConfigOnMaster0,
k.SSHKeyGenAndCopyIDToHosts,
k.GenerateCert,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of configuring private image repositories will be removed from the runtime pkg after we refactor run process, and i think k8s runtime also need those logic. see PR #1621

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! will adapt it when new code merged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that we do this before the code merging. Or at least you add a TODO comment

// TODO: move all these registry operation to the specific registry packages.

//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
Copy link
Member

@kakaZhou719 kakaZhou719 Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this code is from the k0s project, please keep the k0s license. Or could we use those struct directly ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me keep original license.

return c.convertIPVSToAddress(clusterFile)
}

func (c *K0sConfig) convertIPVSToAddress(clusterFile *v2.Cluster) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the logic here seems to be parsing the host configuration information in the *v2.Cluster structure. Do we need to rename this function? convertClusterHosts ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This func mainly use sealer cluster file spec Hosts:IPS as input and out put k0s cluster spec Hosts:SSH:Address, so i named this. If there is a better way to rename it? I think though convertClusterHosts seems like convert some thing in Hosts field, but it don't represent we convert Hosts every field under Hosts.

return err
}
if err := osi.NewAtomicWriter(rootfs + "/k0sctl.yaml").WriteFile(marshal); err != nil {
return fmt.Errorf("write k0sctl config error: %s", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failed to write k0sctl config: %v ,err

}
}
}
return 0, fmt.Errorf("ssh port parse failed")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failed to parse ssh port

}

func (c *K0sConfig) joinK0sHosts(ipList []string, hosts []v2.Host, ssh v1.SSH, role string) error {
for _, masterIP := range ipList {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

masterIP-->> ip


// parseSSHPortByIP parse cluster file ssh port to k0s ssh port.
func (c *K0sConfig) parseSSHPortByIP(ipAddr string, hosts []v2.Host, ssh v1.SSH) (int, error) {
for _, host := range hosts {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here should be able to simplify。
1.convert hosts to map[string]v1.SSH with ssh arg.
2. get map value from ipAddr.

@allencloud
Copy link
Member

Good job, 👍 I think @sealerio/sealer-maintainers could review this first, and @starComingup can illustrate this pull request in the maintainer's meeting this night.


// addHostField generate k0s config spec Host and append it to config file.
func (c *K0sConfig) addHostField(ipAddr string, port int, role, user string) {
c2 := c.Spec.Hosts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just

host := cluster.Host{
      xxx
}
c.Spec.Hosts = append(c.Spec.Hosts, host)

? @starComingup

}

// Cluster describes launchpad.yaml configuration
type Cluster struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afraid that we do not think this struct definition. Just make

type K0sConfig struct{
         APIVersion string           `yaml:"apiVersion"`
	Kind       string           `yaml:"kind"`
	Metadata   *ClusterMetadata `yaml:"metadata"`
	Spec       *cluster.Spec    `yaml:"spec"`
}

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This struct is copy from k0s project. if we need to change it?

*sync.Mutex
// cluster is sealer clusterFile
cluster *v2.Cluster
*Config
Copy link
Member

@allencloud allencloud Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no need to set this anonymous field.

just

// Runtime struct is the runtime interface for k0s
type Runtime struct {
	*sync.Mutex
	// cluster is sealer clusterFile
	cluster *v2.Cluster
        Vlog      int
	RegConfig *registry.Config
	// 	clusterFileK0sConfig is k0sctl cluster config file
	K0sConfig *k0sctl.K0sConfig
}


// Runtime struct is the runtime interface for k0s
type Runtime struct {
*sync.Mutex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see any place where used this Mutex. I am wondering if this is redundant? @starComingup

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy form k8s, now remove it!

return fmt.Errorf("hosts spec cannot be empty")
}
if k.cluster.GetMaster0IP() == nil {
return fmt.Errorf("controller hosts ip cannot be empty")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error message seems to be not so readable. Just usemaster0 IP cannot be empty. Since no one could recognize what does controller mean.

@allencloud
Copy link
Member

Good job for the k0s initial work. 👍

I think there is still more work to do, maybe in the follow-up pull requests:

  • k0s testing;
  • other operations implementation about k0s.

LGTM unless all review comments has been finished. and wish to see that we could iterate quickly with small steps. @starComingup


k.K0sConfig.DefineConfigFork0s(string(output), k.RegConfig.Domain, k.RegConfig.Port, k.cluster.Name)

//write k0sctl.yaml to master0 rootfs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name of this func explicitly illustrates its ability. Maybe we don't have to annotate it. (It's ok to keep it.)

}
c.Spec = &cluster.Spec{}

type clusterConfig Cluster
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird

return nil, err
}

if logrus.GetLevel() == logrus.DebugLevel {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about create a function for level transform?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be fine, more readable.

const (
SeaHub = "sea.hub"
RemoteAddEtcHosts = "cat /etc/hosts |grep '%s' || echo '%s' >> /etc/hosts"
ContainerdLoginCommand = "nerdctl login -u %s -p %s %s"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a general command? I think its readability fails to be better than writing in the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will use by join phase, so i define it as a const.

Signed-off-by: starComingup <[email protected]>
@allencloud
Copy link
Member

What is more, I think we could add a design document about different cluster runtime support into the sealer docs directory, which could also be a follow-up pull request. @starComingup

@starComingup
Copy link
Member Author

What is more, I think we could add a design document about different cluster runtime support into the sealer docs directory, which could also be a follow-up pull request. @starComingup

OK, I will keep this in mind and after k0s、k3s all finished and stable, I would like to design it with use cases.

@allencloud
Copy link
Member

LGTM, and this is related to sealerio/basefs#23 . Please help complete the k3s other part. @starComingup

@allencloud allencloud merged commit d2ad51f into sealerio:main Aug 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants