Skip to content

Commit

Permalink
Fix errors from lint-install run.
Browse files Browse the repository at this point in the history
Fix all the errors reported from lint-install
cd tink-docker; ../out/linters/golangci-lint-v1.42.0 run
cd bootkit; ../out/linters/golangci-lint-v1.42.0 run

Test:
make image

Signed-off-by: Raj-Dharwadkar <[email protected]>
  • Loading branch information
Raj-Dharwadkar committed Aug 27, 2021
1 parent 6538653 commit 84ae53f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 67 deletions.
101 changes: 41 additions & 60 deletions bootkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
}

cmdlines := strings.Split(string(content), " ")
cfg, _ := parsecmdline(cmdlines)
cfg := parsecmdline(cmdlines)

// Get the ID from the metadata service
err = cfg.MetaDataQuery()
Expand Down Expand Up @@ -131,7 +131,11 @@ func main() {
if err != nil {
panic(err)
}
io.Copy(os.Stdout, out)

_, err = io.Copy(os.Stdout, out)
if err != nil {
panic(err)
}

resp, err := cli.ContainerCreate(ctx, tinkContainer, tinkHostConfig, nil, nil, "")
if err != nil {
Expand All @@ -145,74 +149,51 @@ func main() {
fmt.Println(resp.ID)
}

func parsecmdline(cmdlines []string) (cfg tinkConfig, err error) {

// parsecmdline will parse the command line.
func parsecmdline(cmdlines []string) (cfg tinkConfig) {
for i := range cmdlines {
cmdline := strings.Split(cmdlines[i], "=")
if len(cmdline) != 0 {

// Find Registry configuration
if cmdline[0] == "docker_registry" {
cfg.registry = cmdline[1]
}
if cmdline[0] == "registry_username" {
cfg.username = cmdline[1]
}
if cmdline[0] == "registry_password" {
cfg.password = cmdline[1]
}

// Find Tinkerbell servers settings
if cmdline[0] == "packet_base_url" {
cfg.baseURL = cmdline[1]
}
if cmdline[0] == "tinkerbell" {
cfg.tinkerbell = cmdline[1]
}

// Find GRPC configuration
if cmdline[0] == "grpc_authority" {
cfg.grpcAuthority = cmdline[1]
}
if cmdline[0] == "grpc_cert_url" {
cfg.grpcCertURL = cmdline[1]
}

// Find the worker configuration
if cmdline[0] == "worker_id" {
cfg.workerID = cmdline[1]
}
if len(cmdline) == 0 {
continue
}
}
return
}

// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {
// Find Registry configuration
if cmdline[0] == "docker_registry" {
cfg.registry = cmdline[1]
}
if cmdline[0] == "registry_username" {
cfg.username = cmdline[1]
}
if cmdline[0] == "registry_password" {
cfg.password = cmdline[1]
}

// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Find Tinkerbell servers settings
if cmdline[0] == "packet_base_url" {
cfg.baseURL = cmdline[1]
}
if cmdline[0] == "tinkerbell" {
cfg.tinkerbell = cmdline[1]
}

// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Find GRPC configuration
if cmdline[0] == "grpc_authority" {
cfg.grpcAuthority = cmdline[1]
}
if cmdline[0] == "grpc_cert_url" {
cfg.grpcCertURL = cmdline[1]
}

// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
// Find the worker configuration
if cmdline[0] == "worker_id" {
cfg.workerID = cmdline[1]
}
}
return cfg
}

// MetaDataQuery will query the metadata
// MetaDataQuery will query the metadata.
func (cfg *tinkConfig) MetaDataQuery() error {

spaceClient := http.Client{
Timeout: time.Second * 60, // Timeout after 60 seconds (seems massively long is this dial-up?)
}
Expand Down
12 changes: 5 additions & 7 deletions tink-docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func main() {
fmt.Println("Starting Tink-Docker")
go rebootWatch()

// Parse the cmdline in order to find the urls for the repostiory and path to the cert
// Parse the cmdline in order to find the urls for the repository and path to the cert
content, err := ioutil.ReadFile("/proc/cmdline")
if err != nil {
panic(err)
}
cmdlines := strings.Split(string(content), " ")
cfg, _ := parsecmdline(cmdlines)
cfg := parsecmdline(cmdlines)

path := fmt.Sprintf("/etc/docker/certs.d/%s/", cfg.registry)

Expand All @@ -55,8 +55,7 @@ func main() {
}
}

func parsecmdline(cmdlines []string) (cfg tinkConfig, err error) {

func parsecmdline(cmdlines []string) (cfg tinkConfig) {
for i := range cmdlines {
cmdline := strings.Split(cmdlines[i], "=")
if len(cmdline) != 0 {
Expand All @@ -71,13 +70,12 @@ func parsecmdline(cmdlines []string) (cfg tinkConfig, err error) {
}
}
}
return
return cfg
}

// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {

// Get the data
resp, err := http.Get(url)
if err != nil {
Expand All @@ -103,7 +101,7 @@ func rebootWatch() {
// Forever loop
for {
if fileExists("/worker/reboot") {
//Build the command, and execute
// Build the command, and execute
cmd := exec.Command("/sbin/reboot")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down

0 comments on commit 84ae53f

Please sign in to comment.