Skip to content

Commit

Permalink
Merge pull request #37 from Hanna623/dev
Browse files Browse the repository at this point in the history
Add Print loxilb version #28
  • Loading branch information
inhogog2 authored Jan 22, 2025
2 parents 9b7e2e2 + 19935ed commit ddbe754
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SHELL := /bin/bash
loxilbid=$(shell docker ps -f name=$(dock) | grep -w $(dock) | cut -d " " -f 1 | grep -iv "CONTAINER")

build:
@go build -o ${bin} -ldflags="-X 'loxicmd/cmd.BuildInfo=${shell date '+%Y_%m_%d'}-${shell git branch --show-current}-$(shell git show --pretty=format:%h --no-patch)' -X 'loxicmd/cmd.Version=${shell git describe --tags --abbrev=0}'"
@go build -o ${bin} -ldflags="-X 'loxicmd/cmd.BuildInfo=${shell date '+%Y_%m_%d'}-${shell git branch --show-current}-$(shell git show --pretty=format:%h --no-patch)'"

test:
go test
Expand Down
2 changes: 2 additions & 0 deletions cmd/create/create_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func NewCreateLoadBalancerCmd(restOptions *api.RESTOptions) *cobra.Command {
persist - select the lb end-point based on sender
n2 - select the lb end-point base on N2 interface params (only available with fullproxy mode)
n3 - select the lb end-point base on N3 interface params
lc - select the lb end-point base on least connection
--mode value options
onearm - LB put LB-IP as srcIP
fullnat - LB put Service IP as scrIP
Expand Down
1 change: 1 addition & 0 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func GetCmd(restOptions *api.RESTOptions) *cobra.Command {
GetCmd.AddCommand(NewGetBGPNeighborCmd(restOptions))
GetCmd.AddCommand(NewGetHaStateCmd(restOptions))
GetCmd.AddCommand(NewGetBFDCmd(restOptions))
GetCmd.AddCommand(NewGetVersionCmd(restOptions))

return GetCmd
}
Expand Down
89 changes: 89 additions & 0 deletions cmd/get/get_lbversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2022 NetLOX Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package get

import (
"context"
"encoding/json"
"fmt"
"io"
"loxicmd/pkg/api"
"net/http"
"time"

"github.com/spf13/cobra"
)

func NewGetVersionCmd(restOptions *api.RESTOptions) *cobra.Command {
var GetLBVersionCmd = &cobra.Command{
Use: "lbversion",
Short: "Get a loxilb version ",
Long: `It shows version in the LoxiLB`,
Aliases: []string{"LBversion", "LBVersion", "llbversion"},
Run: func(cmd *cobra.Command, args []string) {
client := api.NewLoxiClient(restOptions)
ctx := context.TODO()
var cancel context.CancelFunc
if restOptions.Timeout > 0 {
ctx, cancel = context.WithTimeout(context.TODO(), time.Duration(restOptions.Timeout)*time.Second)
defer cancel()
}
resp, err := client.LBVersion().Get(ctx)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return
}
if resp.StatusCode == http.StatusOK {
PrintGetVersionResult(resp, *restOptions)
return
}

},
}

return GetLBVersionCmd
}

func PrintGetVersionResult(resp *http.Response, o api.RESTOptions) {
Versionresp := api.LBVersionGet{}
var data [][]string
resultByte, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error: Failed to read HTTP response: (%s)\n", err.Error())
return
}

if err := json.Unmarshal(resultByte, &Versionresp); err != nil {
fmt.Printf("Error: Failed to unmarshal HTTP response: (%s)\n", err.Error())
return
}

// if json options enable, it print as a json format.
if o.PrintOption == "json" {
resultIndent, _ := json.MarshalIndent(Versionresp, "", " ")
fmt.Println(string(resultIndent))
return
}

// Table Init
table := TableInit()

table.SetHeader(LBVERSION_TITLE)
data = append(data, []string{Versionresp.Version, Versionresp.BuildInfo})

// Rendering the load balance data to table
TableShow(data, table)
}
1 change: 1 addition & 0 deletions cmd/get/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ var (
HASTATE_TITLE = []string{"Instance", "HAState"}
BFD_TITLE = []string{"Instance", "RemoteIP", "State"}
BFD_WIDE_TITLE = []string{"Instance", "RemoteIP", "SourceIP", "Port", "Interval", "Retry Count", "State"}
LBVERSION_TITLE = []string{"LoxiLB Version", "LoxiLB Build Info"}
)
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/spf13/cobra"
)

var Version string = ""
var Version string = "0.9.7-beta"
var BuildInfo string = ""

var VersionCmd = &cobra.Command{
Expand All @@ -39,7 +39,7 @@ var VersionCmd = &cobra.Command{
Long: `It shows Loxicmd version.`,

Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("%s %s\n", Version, BuildInfo)
fmt.Printf("Loxicmd version: %s\nLoxicmd build info: %s\n", Version, BuildInfo)
},
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
loxiBGPNeighResource = "config/bgp/neigh"
loxiStatusResource = "status"
loxiBFDSessionResource = "config/bfd"
loxiVersionResource = "version"
)

type LoxiClient struct {
Expand Down Expand Up @@ -319,3 +320,15 @@ func (l *LoxiClient) BFDSession() *BFDSession {
},
}
}
func (l *LoxiClient) LBVersion() *LBVersion {
return &LBVersion{
CommonAPI: CommonAPI{
restClient: &l.restClient,
requestInfo: RequestInfo{
provider: loxiProvider,
apiVersion: loxiApiVersion,
resource: loxiVersionResource,
},
},
}
}
26 changes: 26 additions & 0 deletions pkg/api/lbversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2022 NetLOX Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package api

type LBVersion struct {
CommonAPI
}

type LBVersionGet struct {
BuildInfo string `json:"buildInfo"`
Version string `json:"version"`
}

0 comments on commit ddbe754

Please sign in to comment.