-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jg/json endpoint #11
jg/json endpoint #11
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,18 @@ | |
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
glog "log" | ||
"net/http" | ||
"os" | ||
|
||
conf "github.com/reactiveops/fairwinds/pkg/config" | ||
"github.com/reactiveops/fairwinds/pkg/kube" | ||
"github.com/reactiveops/fairwinds/pkg/validator" | ||
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
apitypes "k8s.io/apimachinery/pkg/types" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
|
@@ -37,18 +42,54 @@ var FairwindsName = "fairwinds" | |
var log = logf.Log.WithName(FairwindsName) | ||
|
||
func main() { | ||
dashboard := flag.Bool("dashboard", false, "Runs the webserver for Fairwinds dashboard.") | ||
webhook := flag.Bool("webhook", false, "Runs the webhook webserver.") | ||
|
||
var disableWebhookConfigInstaller bool | ||
flag.BoolVar(&disableWebhookConfigInstaller, "disable-webhook-config-installer", false, | ||
"disable the installer in the webhook server, so it won't install webhook configuration resources during bootstrapping") | ||
|
||
flag.Parse() | ||
logf.SetLogger(logf.ZapLogger(false)) | ||
entryLog := log.WithName("entrypoint") | ||
|
||
c, err := conf.ParseFile("config.yml") | ||
if err != nil { | ||
entryLog.Error(err, "parse config err") | ||
return | ||
} | ||
|
||
if *webhook { | ||
startWebhookServer(c, disableWebhookConfigInstaller) | ||
} | ||
|
||
if *dashboard { | ||
startDashboardServer(c) | ||
} | ||
} | ||
|
||
func startDashboardServer(c conf.Configuration) { | ||
http.HandleFunc("/validate", func(w http.ResponseWriter, r *http.Request) { validateHandler(w, r, c) }) | ||
glog.Println("Starting Fairwinds dashboard webserver on port 8080.") | ||
glog.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func validateHandler(w http.ResponseWriter, r *http.Request, c conf.Configuration) { | ||
var results []validator.Results | ||
pods, err := kube.CoreV1API.Pods("").List(metav1.ListOptions{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At some point in the future it would be really nice to group/validate these results by deployment/daemon set/stateful set/etc to simplify the validation and output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah def, i think the next step will be to validate deployments vs pods. |
||
if err != nil { | ||
return | ||
} | ||
glog.Println("pods count:", len(pods.Items)) | ||
for _, pod := range pods.Items { | ||
result := validator.ValidatePods(c, &pod, validator.Results{}) | ||
results = append(results, result) | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(results) | ||
} | ||
|
||
func startWebhookServer(c conf.Configuration, disableWebhookConfigInstaller bool) { | ||
logf.SetLogger(logf.ZapLogger(false)) | ||
entryLog := log.WithName("entrypoint") | ||
|
||
// Setup a Manager | ||
entryLog.Info("setting up manager") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package kube | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
homedir "github.com/mitchellh/go-homedir" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func createClientset() *kubernetes.Clientset { | ||
var err error | ||
var config *rest.Config | ||
kubeconfig := getKubeConfig() | ||
|
||
switch kubeconfig { | ||
case "": | ||
config, err = rest.InClusterConfig() | ||
default: | ||
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
} | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
} | ||
return clientset | ||
} | ||
|
||
func getKubeConfig() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. controller-runtime's |
||
var path string | ||
|
||
if os.Getenv("KUBECONFIG") != "" { | ||
path = os.Getenv("KUBECONFIG") | ||
return path | ||
} | ||
|
||
if home, err := homedir.Dir(); err == nil { | ||
path = filepath.Join(home, ".kube", "config") | ||
} | ||
|
||
if _, err := os.Stat(path); err != nil { | ||
// No kubeconfig exists, therefor return an emtpy string to | ||
// indicate that this web server is running inside the cluster. | ||
return "" | ||
} | ||
return path | ||
} | ||
|
||
var clientset = createClientset() | ||
|
||
// Kubernetes version 1.11 APIs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why specify the specific API version here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah i don't really need to, let me remove that |
||
|
||
// CoreV1API exports the v1 Core API client. | ||
var CoreV1API = clientset.CoreV1() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it valid just to run a single container with both
--webhook
and--dashboard
flags? Both ports could be specified in the container spec.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i can do that, will just need to make a start up script or use a process manager, but that seems easy enough.