Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: DrummyFloyd <[email protected]>
  • Loading branch information
DrummyFloyd committed Oct 28, 2023
1 parent e160d01 commit 186efe7
Show file tree
Hide file tree
Showing 13 changed files with 904 additions and 12 deletions.
80 changes: 68 additions & 12 deletions cmd/k3s/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/kubefirst/runtime/configs"
"github.com/kubefirst/runtime/pkg"
"github.com/kubefirst/runtime/pkg/argocd"
"github.com/kubefirst/runtime/pkg/docker"
"github.com/kubefirst/runtime/pkg/gitClient"
"github.com/kubefirst/runtime/pkg/github"
gitlab "github.com/kubefirst/runtime/pkg/gitlab"
Expand Down Expand Up @@ -110,6 +109,28 @@ func runK3s(cmd *cobra.Command, args []string) error {
return err
}

sshUserFlag, err := cmd.Flags().GetString("ssh-user")
if err != nil {
return err
}

sshKeyPathFlag, err = cmd.Flags().GetString("ssh-key-path")
if err != nil {
return err
}

ipK3sServersFlag, err := cmd.Flags().GetStringSlice("ip-k3s-servers")
if err != nil {
return err
}

ipK3sAgentsFlag, err := cmd.Flags().GetStringSlice("ip-k3s-agents")
if err != nil {
return err
}
// TODO: remove this !!!!!
log.Info().Msgf("%s %s %s %s", sshUserFlag, sshUserFlag, ipK3sAgentsFlag, ipK3sServersFlag)

// If cluster setup is complete, return
clusterSetupComplete := viper.GetBool("kubefirst-checks.cluster-install-complete")
if clusterSetupComplete {
Expand Down Expand Up @@ -140,22 +161,57 @@ func runK3s(cmd *cobra.Command, args []string) error {
if githubOrgFlag != "" && githubUserFlag != "" {
return fmt.Errorf("only one of --github-user or --github-org can be supplied")
}

// WARNING: check if it's done by k3s or not ?
// Check for existing port forwards before continuing
err = k8s.CheckForExistingPortForwards(8080, 8200, 9000, 9094)
if err != nil {
return fmt.Errorf("%s - this port is required to set up your kubefirst environment - please close any existing port forwards before continuing", err.Error())
}
// err = k8s.CheckForExistingPortForwards(8080, 8200, 9000, 9094)
// if err != nil {
// return fmt.Errorf("%s - this port is required to set up your kubefirst environment - please close any existing port forwards before continuing", err.Error())
// }

// Verify Docker is running
dcli := docker.DockerClientWrapper{
Client: docker.NewDockerClient(),
// TODO: refactor to one unique func
// loop over the ip addresses of the k3s servers and check connectivity with sshConnect func
for _, ip := range ipK3sServersFlag {
client, err := sshConnect(sshUserFlag, ip, sshKeyPathFlag)
if err != nil {
return fmt.Errorf("%s - cannot connect to IP provided - please check your ssh key path and user", err.Error())
}
log.Info().Msgf("connected to %s", ip)
// check if /usr/bin/k3s exist with runcommand from toruntimes.go
_, err = runCommand("which k3s", client)
if err != nil {
return fmt.Errorf("k3s not found in %s", ip)
}
closeSSH(client)
}
_, err = dcli.CheckDockerReady()
if err != nil {
return err

// TODO: refactor to one unique func
// loop over the ip addresses of the k3s agents and check connectivity with sshConnect func
for _, ip := range ipK3sAgentsFlag {
client, err := sshConnect(sshUserFlag, ip, sshKeyPathFlag)
if err != nil {
return fmt.Errorf("%s - cannot connect to IP provided - please check your ssh key path and user", err.Error())
}
log.Info().Msgf("connected to %s", ip)
// check if /usr/bin/k3s exist with runcommand from toruntimes.go
_, err = runCommand("which k3s", client)
if err != nil {
return fmt.Errorf("k3s not found in %s", ip)
}
closeSSH(client)
}

// of the k3s agents, and take the first ip as the MASTER where we want to scp the yaml config files to the wanted path
if ip == ipK3sAgentsFlag[0] {
err := scpToRemote(k3sYamlPathFlag, ip+":~/config.yaml", client)
if err != nil {
return fmt.Errorf("%s - cannot scp to IP provided - please check your ssh key path and user/permission", err.Error())
}
log.Info().Msgf("scp k3s config file to %s", ip)



os.Exit(0)

// Global context
var ctx context.Context
ctx, cancelContext = context.WithCancel(context.Background())
Expand Down
14 changes: 14 additions & 0 deletions cmd/k3s/toruntimes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ func runCommand(cmd string, client *ssh.Client) (string, error) {
return stdoutBuf.String(), nil
}

// scp a file to a path into a remote machine
func scpToRemote(localPath string, remotePath string, client *ssh.Client) error {
session, err := client.NewSession()
if err != nil {
return fmt.Errorf("unable to create SSH session: %v", err)
}
defer session.Close()
err = session.Run(fmt.Sprintf("scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i %s %s %s", client.Config().Auth[0].(ssh.PublicKeys).KeyNames()[0], localPath, remotePath))
if err != nil {
return fmt.Errorf("unable to scp %s to %s: %v", localPath, remotePath, err)
}
return nil
}

// close the ssh Client connection with info about the connection
func closeSSH(client *ssh.Client) {
fmt.Printf("Closing SSH connection to %s\n", client.RemoteAddr())
Expand Down
38 changes: 38 additions & 0 deletions vagrant-test/.ssh/id_rsa
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEAptpOX9wkTve5E5qkV8wgGqs7t25BM2EuV3l9AMgGFT5txDyt34TV
VC3+7DcsCBML3j4jY1Fx8bRuS3+pqKkMmivbpy7el6pVBcV/a52KBDXLWmtwzh0cKrkjDV
kl0ljPzMvPCFMggMCZN3wU0JS7attUTQ3xYq6uR9TqDt7p4rJsIjfQcB7feKqaJQ2WU910
9T0CMCR879Z3H5vNkEhOejHNQJ+U0R0WnXsSIsces2gAN7dFPPfYg3w6hs3b2ErsrQC1MO
eKRcT9xczc++fkZO0p88cHs7Q0W0vWVCm4JjfRJi62CcztlGL6YmEffG726Aq2kbQrnVOX
D5i7H4erJlbuyQR9GzJBQp1IZAiYb2x5y+2m9c8Ch2ia/8h/jKkdkW6P1t3Wv4+ujnYfPy
cOz6V8Al902v5wPo6pmxW4yObmp8/1aedi8tRgl6+az+mDaG4inoP+5WoOctqJAuL0BlFW
YutwiAxDMDtQIGqvILs8fQVRm8JcEOByj9wKU/W/AAAFiP+DK2T/gytkAAAAB3NzaC1yc2
EAAAGBAKbaTl/cJE73uROapFfMIBqrO7duQT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DWZYnkgd
Z95JVISSmYjMJNSDOSvLIvPjslSuN4EHSrFPYEWl9K2Y4xJFYS6iNlGllTxzs1EsOczIEl
rTm7D+6Zy/mvXdHm+uU7O9FotkBuCAJv1cU+c6/Qh4RUP5zkhSrnDWKTZ+hNekNrk36QzB
Qf4XBEpSD38dJAty1wiw5awxC8IRoe0Kw+qn7y9HTJ5McwJ28AAADBAM8tN6heahz072F+
tjXYfBtXHy0aWM2dTXROadimB1MM8z3rnd4QiS1ZWzbHTvoQey2O1kqsHRh4pb/m34b4sz
7U9oT3MA5/FKEmv/jo8XmEBCPLc4569myDlnUDMgv9gTtA32jOXX7L+/+MGiYOnHz0Drtl
EcZrlkXkgWHc9e8mK37yQlhwjxcghxQrOIWpAMTg+9E63Dc4hxjHdNs73qwRaQJgEfsfzy
PAgkFtraILzRuknpCWUsbnqwLQ0HmAkQAAAMEAzixioASGX8jDrc8wwergGnitju95Pvts
V68RiNmP3YYJORKrahzXe86LZTw8Xi4skOp9Tr05l0iTohZYIOFc5gu70CiAGDY3WbZPdL
jJNAr0BCxA1HABP0I6QetZ2pbkDmYbcsfEewLmG/ZU4jOqIoJMFk/bqOMQI9bv8NZFC84b
GnfqXlgHe9L+2erOCSAVLI2bHSVyrPGU46eM+uqyKx7CGx0k5CB5CxEQttX3mtdAATxLyb
hZweShsfkOZTlPAAAAD2ptb25uZXRAam1vbm5ldAECAw==
-----END OPENSSH PRIVATE KEY-----
1 change: 1 addition & 0 deletions vagrant-test/.ssh/id_rsa.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCm2k5f3CRO97kTmqRXzCAaqzu3bkEzYS5XeX0AyAYVPm3EPK3fhNVULf7sNywIEwvePiNjUXHxtG5Lf6moqQyaK9unLt6XqlUFxX9rnYoENctaa3DOHRwquSMNWSXSWM/My88IUyCAwJk3fBTQlLtq21RNDfFirq5H1OoO3unismwiN9BwHt94qpolDZZT3XT1PQIwJHzv1ncfm82QSE56Mc1An5TRHRadexIixx6zaAA3t0U899iDfDqGzdvYSuytALUw54pFxP3FzNz75+Rk7SnzxweztDRbS9ZUKbgmN9EmLrYJzO2UYvpiYR98bvboCraRtCudU5cPmLsfh6smVu7JBH0bMkFCnUhkCJhvbHnL7ab1zwKHaJr/yH+MqR2Rbo/W3da/j66Odh8/Jw7PpXwCX3Ta/nA+jqmbFbjI5uanz/Vp52Ly1GCXr5rP6YNobiKeg/7lag5y2okC4vQGUVZi63CIDEMwO1Agaq8guzx9BVGbwlwQ4HKP3ApT9b8= jmonnet@jmonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1695938830
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1000
1 change: 1 addition & 0 deletions vagrant-test/.vagrant/machines/machine1/virtualbox/id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f753599b-e3dc-42e5-9b14-7d81606ac285
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2cc40af2d213404a88c2cb931804e0fe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/home/jmonnet/Documents/01-perso/kubefirst/vagrant-test
9 changes: 9 additions & 0 deletions vagrant-test/.vagrant/rgloader/loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file loads the proper rgloader/loader.rb file that comes packaged
# with Vagrant so that encoded files can properly run with Vagrant.

if ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]
require File.expand_path(
"rgloader/loader", ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"])
else
raise "Encoded files can't be read outside of the Vagrant installer."
end
45 changes: 45 additions & 0 deletions vagrant-test/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
MASTER_COUNT = 3
IMAGE = 'ubuntu/mantic64'
#
# ...
#
# Vagrant.configure("2") do |config|
#
# (1..MASTER_COUNT).each do |i|
# config.ssh.insert_key = false
# config.vm.define "kubemaster#{i}" do |kubemasters|
# kubemasters.vm.box = IMAGE
# kubemasters.vm.hostname = "kubemaster#{i}"
# kubemasters.vm.network :private_network, ip: "10.0.0.#{i+10}"
# kubemasters.vm.provision "file", source: "~/.ssh/id_ed25519.pub", destination: "/tmp/id_ed25519.pub"
# kubemasters.vm.provision "file", source: "~/.ssh/id_ed25519", destination: "/tmp/id_ed25519"
# kubemasters.vm.provision "shell", privileged: true, path: "scripts/master_install.sh"
# end
# end
#
# end
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
# Configuration de base pour toutes les machines
config.vm.box = IMAGE

# Configuration de la clé SSH

# Configuration des machines individuelles
(1..MASTER_COUNT).each do |i|
config.vm.define "kubemaster#{i}" do |machine|
machine.vm.network :private_network, type: 'static', ip: "10.0.0.#{10 + i}"
machine.vm.hostname = "kubemaster#{i}"
machine.vm.provision 'file', source: '~/.ssh/id_ed25519.pub', destination: '/tmp/id_ed25519.pub'
machine.vm.provision 'shell', privileged: true, inline: <<-SHELL
echo $(cat /tmp/id_ed25519.pub) >> /home/vagrant/.ssh/authorized_keys
echo "10.0.0.11 kubemaster1" >>/etc/hosts
echo "10.0.0.12 kubemaster2" >>/etc/hosts
echo "10.0.0.13 kubemaster3" >>/etc/hosts
SHELL
end
end
end
17 changes: 17 additions & 0 deletions vagrant-test/scripts/master_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
# based on https://tferdinand.net/creer-un-cluster-kubernetes-local-avec-vagrant/
# Deploy keys to allow all nodes to connect each others as root
mv /tmp/id_ed25519* /root/.ssh/
#
chmod 400 /root/.ssh/id_ed25519*
chown root:root /root/.ssh/id_ed25519*
#
cat /root/.ssh/id_ed25519.pub >>/root/.ssh/authorized_keys
chmod 400 /root/.ssh/authorized_keys
chown root:root /root/.ssh/authorized_keys

# Add current node in /etc/hosts
echo "127.0.1.1 $(hostname)" >>/etc/hosts

# If we are on first node, launch k3s with cluster-init, else we join the existing cluster
# curl -sfL https://get.k3s.io | sh -
Loading

0 comments on commit 186efe7

Please sign in to comment.