Skip to content

Commit

Permalink
Add report utils for gke-diagnoser to support json output
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixiansong committed Mar 20, 2023
1 parent b179798 commit bb14b6e
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cmd/gke-diagnoser/app/util/report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2023 The Kubernetes Authors.
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 util

import (
"encoding/json"
"fmt"
)

const (
//JSONOutput is the constant value for output type JSON
JSONOutput string = "json"
)

// Report represents the final output of the analyzer
type Report struct {
Resources []*Resource `json:"resources"`
}

// Resource represents the a resource of the cluster and all the checks done on it
type Resource struct {
Kind string `json:"kind"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Checks []*Check `json:"checks"`
}

// Check represents the result of a check
type Check struct {
Name string `json:"name"`
Message string `json:"message"`
Result string `json:"result"`
}

func JsonReport(report *Report) (string, error) {
jsonRaw, err := json.MarshalIndent(report, "", " ")
if err != nil {
return "", fmt.Errorf("Error Marshalling JSON: %v", err)
}
return string(jsonRaw), nil
}

// SupportedOutputs returns a string list of output formats supported by this package
func SupportedOutputs() []string {
return []string{
JSONOutput,
}
}

0 comments on commit bb14b6e

Please sign in to comment.