Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .bingo/Variables.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ GOHOSTARM ?= $(shell $(GO) env GOHOSTARM)
# @echo "Running golangci-lint"
# @$(GOLANGCI_LINT) <flags/args..>
#
GOLANGCI_LINT := $(GOBIN)/golangci-lint-v1.64.8
GOLANGCI_LINT := $(GOBIN)/golangci-lint-v2.7.0
$(GOLANGCI_LINT): $(BINGO_DIR)/golangci-lint.mod
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
@echo "(re)installing $(GOBIN)/golangci-lint-v1.64.8"
@cd $(BINGO_DIR) && GOWORK=off GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) GOARM=$(GOHOSTARM) $(GO) build -mod=mod -modfile=golangci-lint.mod -o=$(GOBIN)/golangci-lint-v1.64.8 "github.com/golangci/golangci-lint/cmd/golangci-lint"
@echo "(re)installing $(GOBIN)/golangci-lint-v2.7.0"
@cd $(BINGO_DIR) && GOWORK=off GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) GOARM=$(GOHOSTARM) $(GO) build -mod=mod -modfile=golangci-lint.mod -o=$(GOBIN)/golangci-lint-v2.7.0 "github.com/golangci/golangci-lint/v2/cmd/golangci-lint"

GOTESTSUM := $(GOBIN)/gotestsum-v1.13.0
$(GOTESTSUM): $(BINGO_DIR)/gotestsum.mod
Expand Down
4 changes: 2 additions & 2 deletions .bingo/golangci-lint.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT

go 1.24
go 1.24.0

require github.com/golangci/golangci-lint v1.64.8 // cmd/golangci-lint
require github.com/golangci/golangci-lint/v2 v2.7.0 // cmd/golangci-lint
221 changes: 221 additions & 0 deletions .bingo/golangci-lint.sum

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: 2
issues:
exclude-dirs:
- pkg/api/openapi # Generated code
5 changes: 3 additions & 2 deletions Dockerfile.openapi
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM openapitools/openapi-generator-cli:v7.16.0

RUN apt-get update
RUN apt-get install -y make sudo git wget
# -o APT::Sandbox::User=root is a workaround for rootless podman setgroups error in Prow env
RUN apt-get -o APT::Sandbox::User=root update
RUN apt-get -o APT::Sandbox::User=root install -y make sudo git wget

# Install Go 1.24
RUN wget https://go.dev/dl/go1.24.0.linux-amd64.tar.gz && \
Expand Down
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ verify: check-gopath
# Runs our linter to verify that everything is following best practices
# Linter is set to ignore `unused` stuff due to example being incomplete by definition
lint: $(GOLANGCI_LINT)
$(GOLANGCI_LINT) run -e unused \
./cmd/... \
./pkg/...
$(GOLANGCI_LINT) run ./...
.PHONY: lint

# Build binaries
Expand Down
6 changes: 5 additions & 1 deletion cmd/hyperfleet-api/environments/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ func (e *Env) Teardown() {
glog.Errorf("Error closing database session factory: %s", err.Error())
}
}
e.Clients.OCM.Close()
if e.Clients.OCM != nil {
if err := e.Clients.OCM.Close(); err != nil {
glog.Errorf("Error closing OCM client: %v", err)
}
}
}

func setConfigDefaults(flags *pflag.FlagSet, defaults map[string]string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/hyperfleet-api/environments/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestLoadServices(t *testing.T) {
return
}

s := reflect.ValueOf(env.Services)
s := reflect.ValueOf(&env.Services).Elem()
sType := s.Type()

for i := 0; i < s.NumField(); i++ {
Expand Down
4 changes: 3 additions & 1 deletion cmd/hyperfleet-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func main() {
// This is needed to make `glog` believe that the flags have already been parsed, otherwise
// every log messages is prefixed by an error message stating the the flags haven't been
// parsed.
_ = flag.CommandLine.Parse([]string{})
if err := flag.CommandLine.Parse([]string{}); err != nil {
glog.Fatalf("Failed to parse flags: %v", err)
}

//pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

Expand Down
4 changes: 3 additions & 1 deletion cmd/hyperfleet-api/server/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ func (s apiServer) Start() {
// after the server exits but before the application terminates
// we need to explicitly close Go's sql connection pool.
// this needs to be called *exactly* once during an app's lifetime.
env().Database.SessionFactory.Close()
if err := env().Database.SessionFactory.Close(); err != nil {
glog.Errorf("Error closing database connection: %v", err)
}
}

func (s apiServer) Stop() error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ import (
func RequestLoggingMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
path := strings.TrimSuffix(request.URL.Path, "/")
doLog := true

// these contribute greatly to log spam but are not useful or meaningful.
// consider a list/map of URLs should this grow in the future.
if path == "/api/hyperfleet" {
doLog = false
}
doLog := path != "/api/hyperfleet"

loggingWriter := NewLoggingWriter(writer, request, NewJSONLogFormatter())

Expand Down
2 changes: 1 addition & 1 deletion cmd/hyperfleet-api/server/logging/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (writer *loggingWriter) prepareRequestLog() (string, error) {

func (writer *loggingWriter) prepareResponseLog(elapsed string) (string, error) {
info := &ResponseInfo{
Header: writer.ResponseWriter.Header(),
Header: writer.Header(),
Body: writer.responseBody,
Status: writer.responseStatus,
Elapsed: elapsed,
Expand Down
11 changes: 6 additions & 5 deletions cmd/hyperfleet-api/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ func (s *apiServer) routes() *mux.Router {
}

authzMiddleware := auth.NewAuthzMiddlewareMock()
if env().Config.Server.EnableAuthz {
// TODO: authzMiddleware, err = auth.NewAuthzMiddleware()
// check(err, "Unable to create authz middleware")
}

// TODO: Create issue to track enabling authorization middleware
// if env().Config.Server.EnableAuthz {
// var err error
// authzMiddleware, err = auth.NewAuthzMiddleware()
// check(err, "Unable to create authz middleware")
// }
// mainRouter is top level "/"
mainRouter := mux.NewRouter()
mainRouter.NotFoundHandler = http.HandlerFunc(api.SendNotFound)
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/Masterminds/squirrel v1.1.0
github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b
github.com/bxcodec/faker/v3 v3.2.0
github.com/docker/go-connections v0.6.0
github.com/docker/go-healthcheck v0.1.0
github.com/ghodss/yaml v1.0.0
github.com/go-gormigrate/gormigrate/v2 v2.0.0
Expand All @@ -29,6 +30,7 @@ require (
github.com/testcontainers/testcontainers-go v0.33.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.33.0
github.com/yaacov/tree-search-language v0.0.0-20190923184055-1c2dad2e354b
go.uber.org/mock v0.6.0
gopkg.in/resty.v1 v1.12.0
gorm.io/datatypes v1.2.7
gorm.io/driver/postgres v1.6.0
Expand All @@ -54,7 +56,6 @@ require (
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v28.5.1+incompatible // indirect
github.com/docker/go-connections v0.6.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.3 // indirect
Expand Down Expand Up @@ -112,15 +113,12 @@ require (
go.opentelemetry.io/otel/metric v1.38.0 // indirect
go.opentelemetry.io/otel/sdk v1.38.0 // indirect
go.opentelemetry.io/otel/trace v1.38.0 // indirect
go.uber.org/mock v0.6.0 // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/mod v0.27.0 // indirect
golang.org/x/net v0.43.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/text v0.29.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.36.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20251014184007-4626949a642f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20251014184007-4626949a642f // indirect
google.golang.org/grpc v1.75.1 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,6 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -898,8 +896,6 @@ golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg=
golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
3 changes: 1 addition & 2 deletions openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "5.4.0"
"version": "7.16.0"
}
}

36 changes: 26 additions & 10 deletions pkg/api/adapter_status_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ func (as *AdapterStatus) ToOpenAPI() *openapi.AdapterStatus {
// Unmarshal Conditions
var conditions []openapi.AdapterCondition
if len(as.Conditions) > 0 {
_ = json.Unmarshal(as.Conditions, &conditions)
if err := json.Unmarshal(as.Conditions, &conditions); err != nil {
// If unmarshal fails, use empty slice
conditions = []openapi.AdapterCondition{}
}
}

// Unmarshal Data
var data map[string]map[string]interface{}
if len(as.Data) > 0 {
_ = json.Unmarshal(as.Data, &data)
if err := json.Unmarshal(as.Data, &data); err != nil {
// If unmarshal fails, use empty map
data = make(map[string]map[string]interface{})
}
}

// Unmarshal Metadata
Expand Down Expand Up @@ -113,20 +119,30 @@ func AdapterStatusFromOpenAPICreate(
}
}

// Marshal Conditions
conditionsJSON, _ := json.Marshal(adapterConditions)
// Marshal Conditions - if this fails, it's a programming error
conditionsJSON, err := json.Marshal(adapterConditions)
if err != nil {
// Fallback to empty array JSON
// Log marshal failure - this indicates a programming error
// logger.Errorf("Failed to marshal adapter conditions: %v", err)
conditionsJSON = []byte("[]")
}

dataJSON := []byte("{}") // default fallback

// Marshal Data
data := make(map[string]map[string]interface{})
if req.Data != nil {
data = req.Data
if b, err := json.Marshal(req.Data); err == nil {
dataJSON = b
}
}
dataJSON, _ := json.Marshal(data)

// Marshal Metadata (if provided)
var metadataJSON datatypes.JSON
metadataJSON := []byte("{}") // safe fallback

if req.Metadata != nil {
metadataJSON, _ = json.Marshal(req.Metadata)
if b, err := json.Marshal(req.Metadata); err == nil {
metadataJSON = b
}
}

return &AdapterStatus{
Expand Down
27 changes: 20 additions & 7 deletions pkg/api/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,25 @@ func (c *Cluster) BeforeCreate(tx *gorm.DB) error {
// ToOpenAPI converts to OpenAPI model
func (c *Cluster) ToOpenAPI() *openapi.Cluster {
// Unmarshal Spec
var spec map[string]interface{}
spec := make(map[string]interface{})
if len(c.Spec) > 0 {
_ = json.Unmarshal(c.Spec, &spec)
}

// Unmarshal Labels
var labels map[string]string
if len(c.Labels) > 0 {
_ = json.Unmarshal(c.Labels, &labels)
if err := json.Unmarshal(c.Labels, &labels); err != nil {
labels = make(map[string]string)
}
}

// Unmarshal StatusConditions
// Unmarshal StatusConditions (stored as ResourceCondition in DB)
var statusConditions []openapi.ResourceCondition
if len(c.StatusConditions) > 0 {
_ = json.Unmarshal(c.StatusConditions, &statusConditions)
if err := json.Unmarshal(c.StatusConditions, &statusConditions); err != nil {
statusConditions = []openapi.ResourceCondition{}
}
}

// Generate Href if not set (fallback)
Expand Down Expand Up @@ -129,17 +133,26 @@ func (c *Cluster) ToOpenAPI() *openapi.Cluster {
// ClusterFromOpenAPICreate creates GORM model from OpenAPI CreateRequest
func ClusterFromOpenAPICreate(req *openapi.ClusterCreateRequest, createdBy string) *Cluster {
// Marshal Spec
specJSON, _ := json.Marshal(req.Spec)
specJSON, err := json.Marshal(req.Spec)
if err != nil {
specJSON = []byte("{}")
}

// Marshal Labels
labels := make(map[string]string)
if req.Labels != nil {
labels = *req.Labels
}
labelsJSON, _ := json.Marshal(labels)
labelsJSON, err := json.Marshal(labels)
if err != nil {
labelsJSON = []byte("{}")
}

// Marshal empty StatusConditions
statusConditionsJSON, _ := json.Marshal([]openapi.ResourceCondition{})
statusConditionsJSON, err := json.Marshal([]openapi.ResourceCondition{})
if err != nil {
statusConditionsJSON = []byte("[]")
}

return &Cluster{
Kind: req.Kind,
Expand Down
30 changes: 23 additions & 7 deletions pkg/api/node_pool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,25 @@ func (np *NodePool) ToOpenAPI() *openapi.NodePool {
// Unmarshal Spec
var spec map[string]interface{}
if len(np.Spec) > 0 {
_ = json.Unmarshal(np.Spec, &spec)
if err := json.Unmarshal(np.Spec, &spec); err != nil {
spec = make(map[string]interface{})
}
}

// Unmarshal Labels
var labels map[string]string
if len(np.Labels) > 0 {
_ = json.Unmarshal(np.Labels, &labels)
if err := json.Unmarshal(np.Labels, &labels); err != nil {
labels = make(map[string]string)
}
}

// Unmarshal StatusConditions
// Unmarshal StatusConditions (stored as ResourceCondition in DB)
var statusConditions []openapi.ResourceCondition
if len(np.StatusConditions) > 0 {
_ = json.Unmarshal(np.StatusConditions, &statusConditions)
if err := json.Unmarshal(np.StatusConditions, &statusConditions); err != nil {
statusConditions = []openapi.ResourceCondition{}
}
}

// Generate Href if not set (fallback)
Expand Down Expand Up @@ -150,17 +156,27 @@ func (np *NodePool) ToOpenAPI() *openapi.NodePool {
// NodePoolFromOpenAPICreate creates GORM model from OpenAPI CreateRequest
func NodePoolFromOpenAPICreate(req *openapi.NodePoolCreateRequest, ownerID, createdBy string) *NodePool {
// Marshal Spec
specJSON, _ := json.Marshal(req.Spec)
specJSON, err := json.Marshal(req.Spec)
if err != nil {
//logger.Errorf("Failed to marshal NodePool spec: %v", err)
specJSON = []byte("{}")
}

// Marshal Labels
labels := make(map[string]string)
if req.Labels != nil {
labels = *req.Labels
}
labelsJSON, _ := json.Marshal(labels)
labelsJSON, err := json.Marshal(labels)
if err != nil {
labelsJSON = []byte("{}")
}

// Marshal empty StatusConditions
statusConditionsJSON, _ := json.Marshal([]openapi.ResourceCondition{})
statusConditionsJSON, err := json.Marshal([]openapi.ResourceCondition{})
if err != nil {
statusConditionsJSON = []byte("[]")
}

kind := "NodePool"
if req.Kind != nil {
Expand Down
Loading