From fc133f328ac8d13e6000c57633f50be1e1126f72 Mon Sep 17 00:00:00 2001 From: Christoph Kluge Date: Wed, 29 Mar 2023 10:39:31 +0200 Subject: [PATCH] Add check to ccms getSubCluster routine - Fixes nil pointer panic if topology requested from undef'd subcluster - rest.go: vscode LINT --- internal/api/rest.go | 14 +++++++------- internal/metricdata/cc-metric-store.go | 7 ++++++- pkg/archive/clusterConfig.go | 7 +++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/internal/api/rest.go b/internal/api/rest.go index 3525fba..c4af330 100644 --- a/internal/api/rest.go +++ b/internal/api/rest.go @@ -99,12 +99,12 @@ type StartJobApiResponse struct { // @Description Request to stop running job using stoptime and final state. // @Description They are only required if no database id was provided with endpoint. type StopJobApiRequest struct { - // Stop Time of job as epoch + // Stop Time of job as epoch StopTime int64 `json:"stopTime" validate:"required" example:"1649763839"` - State schema.JobState `json:"jobState" validate:"required" example:"completed" enums:"completed,failed,cancelled,stopped,timeout"` // Final job state - JobId *int64 `json:"jobId" example:"123000"` // Cluster Job ID of job - Cluster *string `json:"cluster" example:"fritz"` // Cluster of job - StartTime *int64 `json:"startTime" example:"1649723812"` // Start Time of job as epoch + State schema.JobState `json:"jobState" validate:"required" example:"completed" enums:"completed,failed,cancelled,stopped,timeout"` // Final job state + JobId *int64 `json:"jobId" example:"123000"` // Cluster Job ID of job + Cluster *string `json:"cluster" example:"fritz"` // Cluster of job + StartTime *int64 `json:"startTime" example:"1649723812"` // Start Time of job as epoch } // ErrorResponse model @@ -723,13 +723,13 @@ func (api *RestApi) updateUser(rw http.ResponseWriter, r *http.Request) { delrole := r.FormValue("remove-role") // TODO: Handle anything but roles... - if (newrole != "") { + if newrole != "" { if err := api.Authentication.AddRole(r.Context(), mux.Vars(r)["id"], newrole); err != nil { http.Error(rw, err.Error(), http.StatusUnprocessableEntity) return } rw.Write([]byte("Add Role Success")) - } else if (delrole != "") { + } else if delrole != "" { if err := api.Authentication.RemoveRole(r.Context(), mux.Vars(r)["id"], delrole); err != nil { http.Error(rw, err.Error(), http.StatusUnprocessableEntity) return diff --git a/internal/metricdata/cc-metric-store.go b/internal/metricdata/cc-metric-store.go index 62617b0..b50e885 100644 --- a/internal/metricdata/cc-metric-store.go +++ b/internal/metricdata/cc-metric-store.go @@ -260,9 +260,14 @@ func (ccms *CCMetricStore) buildQueries( scopes []schema.MetricScope) ([]ApiQuery, []schema.MetricScope, error) { queries := make([]ApiQuery, 0, len(metrics)*len(scopes)*len(job.Resources)) - topology := archive.GetSubCluster(job.Cluster, job.SubCluster).Topology assignedScope := []schema.MetricScope{} + subcluster, scerr := archive.GetSubCluster(job.Cluster, job.SubCluster) + if scerr != nil { + return nil, nil, scerr + } + topology := subcluster.Topology + for _, metric := range metrics { remoteName := ccms.toRemoteName(metric) mc := archive.GetMetricConfig(job.Cluster, metric) diff --git a/pkg/archive/clusterConfig.go b/pkg/archive/clusterConfig.go index d14a5ae..21c9c41 100644 --- a/pkg/archive/clusterConfig.go +++ b/pkg/archive/clusterConfig.go @@ -78,18 +78,17 @@ func GetCluster(cluster string) *schema.Cluster { return nil } -func GetSubCluster(cluster, subcluster string) *schema.SubCluster { - +func GetSubCluster(cluster, subcluster string) (*schema.SubCluster, error) { for _, c := range Clusters { if c.Name == cluster { for _, p := range c.SubClusters { if p.Name == subcluster { - return p + return p, nil } } } } - return nil + return nil, fmt.Errorf("Subcluster '%v' not found for cluster '%v', or cluster '%v' not configured!", subcluster, cluster, cluster) } func GetMetricConfig(cluster, metric string) *schema.MetricConfig {