-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add report utils for gke-diagnoser to support json output
- Loading branch information
1 parent
b179798
commit bb14b6e
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |