-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CNS checks apiserver in healthz
- Loading branch information
1 parent
281770b
commit b78005a
Showing
3 changed files
with
57 additions
and
1 deletion.
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,54 @@ | ||
package healthserver | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha" | ||
"github.com/pkg/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/healthz" | ||
) | ||
|
||
var schema = runtime.NewScheme() | ||
|
||
func init() { | ||
utilruntime.Must(v1alpha.AddToScheme(schema)) | ||
} | ||
|
||
func NewHealthzHandlerWithChecks() http.Handler { | ||
cfg, err := ctrl.GetConfig() | ||
if err != nil { | ||
panic(err) | ||
} | ||
cli, err := client.New(cfg, client.Options{ | ||
Scheme: schema, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
checks := map[string]healthz.Checker{ | ||
"nnc": func(req *http.Request) error { | ||
ctx := req.Context() | ||
// we just care that we're allowed to List NNCs so set limit to 1 to minimize | ||
// additional load on apiserver | ||
if err := cli.List(ctx, &v1alpha.NodeNetworkConfigList{}, &client.ListOptions{ | ||
Namespace: metav1.NamespaceSystem, | ||
Limit: int64(1), | ||
}); err != nil { | ||
return errors.Wrap(err, "failed to list NodeNetworkConfig") | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
// strip prefix so that it runs through all checks registered on the handler. | ||
// otherwise it will look for a check named "healthz" and return a 404 if not there. | ||
return http.StripPrefix("/healthz", &healthz.Handler{ | ||
Checks: checks, | ||
}) | ||
} |
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
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