main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package inclusterclient
  14. import (
  15. "context"
  16. "crypto/sha256"
  17. "encoding/base64"
  18. "flag"
  19. "fmt"
  20. "log"
  21. "net/http"
  22. "time"
  23. "github.com/spf13/cobra"
  24. "k8s.io/client-go/kubernetes"
  25. "k8s.io/client-go/rest"
  26. "k8s.io/component-base/logs"
  27. "k8s.io/klog"
  28. )
  29. var pollInterval int
  30. // CmdInClusterClient is used by agnhost Cobra.
  31. var CmdInClusterClient = &cobra.Command{
  32. Use: "inclusterclient",
  33. Short: "Periodically poll the Kubernetes \"/healthz\" endpoint",
  34. Long: `Periodically polls the Kubernetes "/healthz" endpoint using the in-cluster config. Because of this, this subcommand is meant to be run inside of a Kubernetes pod.
  35. This subcommand can also be used to validate token rotation.`,
  36. Args: cobra.MaximumNArgs(0),
  37. Run: main,
  38. }
  39. func init() {
  40. CmdInClusterClient.Flags().IntVar(&pollInterval, "poll-interval", 30,
  41. "poll interval of call to /healthz in seconds")
  42. }
  43. func main(cmd *cobra.Command, args []string) {
  44. logs.InitLogs()
  45. defer logs.FlushLogs()
  46. flag.Set("logtostderr", "true")
  47. klog.Infof("started")
  48. cfg, err := rest.InClusterConfig()
  49. if err != nil {
  50. log.Fatalf("err: %v", err)
  51. }
  52. cfg.Wrap(func(rt http.RoundTripper) http.RoundTripper {
  53. return &debugRt{
  54. rt: rt,
  55. }
  56. })
  57. c := kubernetes.NewForConfigOrDie(cfg).RESTClient()
  58. //lint:ignore SA1015 noisy positive, `time.Tick` is used in a main function which is fine
  59. t := time.Tick(time.Duration(pollInterval) * time.Second)
  60. for {
  61. <-t
  62. klog.Infof("calling /healthz")
  63. b, err := c.Get().AbsPath("/healthz").Do(context.TODO()).Raw()
  64. if err != nil {
  65. klog.Errorf("status=failed")
  66. klog.Errorf("error checking /healthz: %v\n%s\n", err, string(b))
  67. }
  68. }
  69. }
  70. type debugRt struct {
  71. rt http.RoundTripper
  72. }
  73. func (rt *debugRt) RoundTrip(req *http.Request) (*http.Response, error) {
  74. authHeader := req.Header.Get("Authorization")
  75. if len(authHeader) != 0 {
  76. authHash := sha256.Sum256([]byte(fmt.Sprintf("%s|%s", "salt", authHeader)))
  77. klog.Infof("authz_header=%s", base64.RawURLEncoding.EncodeToString(authHash[:]))
  78. } else {
  79. klog.Errorf("authz_header=<empty>")
  80. }
  81. return rt.rt.RoundTrip(req)
  82. }
  83. func (rt *debugRt) WrappedRoundTripper() http.RoundTripper { return rt.rt }