Skip to content

Commit

Permalink
Add /version to agent so UI can display it
Browse files Browse the repository at this point in the history
Signed-off-by: Ondra Machacek <[email protected]>
  • Loading branch information
machacekondra committed Nov 15, 2024
1 parent 8b0b09f commit 56041f4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Containerfile.agent
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ RUN go mod download

COPY . .

ARG VERSION
ENV VERSION=${VERSION}

USER 0
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -buildvcs=false -o /planner-agent cmd/planner-agent/main.go
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -buildvcs=false \
-ldflags "-X github.com/kubev2v/migration-planner/internal/agent.version=${VERSION}" \
-o /planner-agent cmd/planner-agent/main.go

FROM registry.access.redhat.com/ubi9/ubi-micro

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ GO_LD_FLAGS := -ldflags "\
-X github.com/kubev2v/migration-planner/pkg/version.commitFromGit=$(SOURCE_GIT_COMMIT) \
-X github.com/kubev2v/migration-planner/pkg/version.gitTreeState=$(SOURCE_GIT_TREE_STATE) \
-X github.com/kubev2v/migration-planner/pkg/version.buildDate=$(BIN_TIMESTAMP) \
-X github.com/kubev2v/migration-planner/internal/agent.version=$(SOURCE_GIT_TAG) \
$(LD_FLAGS)"
GO_BUILD_FLAGS += $(GO_LD_FLAGS)

Expand Down Expand Up @@ -93,7 +94,7 @@ build-agent: bin

# rebuild container only on source changes
bin/.migration-planner-agent-container: bin Containerfile.agent go.mod go.sum $(GO_FILES)
$(PODMAN) build . -f Containerfile.agent -t $(MIGRATION_PLANNER_AGENT_IMAGE):latest
$(PODMAN) build . --build-arg VERSION=$(SOURCE_GIT_TAG) -f Containerfile.agent -t $(MIGRATION_PLANNER_AGENT_IMAGE):latest

bin/.migration-planner-api-container: bin Containerfile.api go.mod go.sum $(GO_FILES)
$(PODMAN) build . -f Containerfile.api -t $(MIGRATION_PLANNER_API_IMAGE):latest
Expand Down
7 changes: 6 additions & 1 deletion internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const (
InventoryFile = "inventory.json"
)

// This varible is set during build time.
// It contains the version of the code.
// For more info take a look into Makefile.
var version string

// New creates a new agent.
func New(log *log.PrefixLogger, config *Config) *Agent {
return &Agent{
Expand All @@ -36,7 +41,7 @@ func (a *Agent) GetLogPrefix() string {

func (a *Agent) Run(ctx context.Context) error {
var err error
a.log.Infof("Starting agent...")
a.log.Infof("Starting agent: %s", version)
defer a.log.Infof("Agent stopped")
a.log.Infof("Configuration: %s", a.config.String())

Expand Down
2 changes: 2 additions & 0 deletions internal/agent/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func (u *InventoryUpdater) updateSourceStatus(ctx context.Context, status api.So
StatusInfo: statusInfo,
Inventory: inventory,
CredentialUrl: u.credUrl,
// TODO: when moving to AgentStatusUpdate put this:
//Version: version,
}

newContents, err := json.Marshal(update)
Expand Down
11 changes: 11 additions & 0 deletions internal/agent/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (
)

func RegisterApi(router *chi.Mux, log *log.PrefixLogger, dataDir string) {
router.Get("/api/v1/version", func(w http.ResponseWriter, r *http.Request) {
_ = render.Render(w, r, VersionReply{Version: version})
})
router.Get("/api/v1/status", func(w http.ResponseWriter, r *http.Request) {
statusHandler(dataDir, w, r)
})
Expand All @@ -40,10 +43,18 @@ type StatusReply struct {
StatusInfo string `json:"statusInfo"`
}

type VersionReply struct {
Version string `json:"version"`
}

func (s StatusReply) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}

func (v VersionReply) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}

func statusHandler(dataDir string, w http.ResponseWriter, r *http.Request) {
status, statusInfo, _ := calculateStatus(dataDir)
_ = render.Render(w, r, StatusReply{Status: string(status), StatusInfo: statusInfo})
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/e2e_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
type PlannerAgent interface {
Run(string) error
Login(url string, user string, pass string) (*http.Response, error)
Version() (string, error)
Remove() error
GetIp() (string, error)
IsServiceRunning(string, string) bool
Expand Down Expand Up @@ -111,6 +112,38 @@ func (p *plannerAgentLibvirt) prepareImage(sourceId string) error {
return nil
}

func (p *plannerAgentLibvirt) Version() (string, error) {
agentIP, err := p.GetIp()
if err != nil {
return "", fmt.Errorf("failed to get agent IP: %w", err)
}
// Create a new HTTP GET request
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s:3333/api/v1/version", agentIP), nil)
if err != nil {
return "", fmt.Errorf("Failed to create request: %v", err)
}

// Set the Content-Type header to application/json
req.Header.Set("Content-Type", "application/json")

// Send the request using http.DefaultClient
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("Failed to send request: %v", err)
}
defer resp.Body.Close()

// Read the response body
var result struct {
Version string `json:"version"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", fmt.Errorf("failed to decode response: %v", err)
}
return result.Version, nil
}

func (p *plannerAgentLibvirt) Login(url string, user string, pass string) (*http.Response, error) {
agentIP, err := p.GetIp()
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,10 @@ var _ = Describe("e2e", func() {
Expect(err).To(BeNil())
Expect(res.StatusCode).To(Equal(http.StatusUnprocessableEntity))
})
It("version endpoint is not empty", func() {
version, err := agent.Version()
Expect(err).To(BeNil())
Expect(version).ToNot(BeEmpty())
})
})
})

0 comments on commit 56041f4

Please sign in to comment.