Skip to content

Commit

Permalink
Remove conversion webhook as we started using dynamic clients (#621)
Browse files Browse the repository at this point in the history
* Remove conversion webhook as we started using dynamic clients

* Make readiness-port to be customized

Add a flag `--readiness-port` in directpv binary to support custom port
for readiness webhook
  • Loading branch information
Praveenrajmani committed Sep 3, 2022
1 parent a8d1ade commit 8da64cc
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 1,364 deletions.
2 changes: 2 additions & 0 deletions cmd/directpv/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
dynamicDriveHandler = false
metricsPort = 10443
disableUDevListener = false
readinessPort = 30443
)

var driverCmd = &cobra.Command{
Expand Down Expand Up @@ -117,6 +118,7 @@ func init() {
driverCmd.Flags().BoolVarP(&dynamicDriveHandler, "dynamic-drive-handler", "", dynamicDriveHandler, "running in dynamic drive handler mode (experimental)")
driverCmd.Flags().IntVarP(&metricsPort, "metrics-port", "", metricsPort, "Metrics port for scraping. default is 10443")
driverCmd.Flags().BoolVarP(&disableUDevListener, "disable-udev-listener", "", disableUDevListener, "disable uevent listener and rely on 30secs internal drive-sync mechanism")
driverCmd.Flags().IntVarP(&readinessPort, "readiness-port", "", readinessPort, "Readiness port. default is 30443")

driverCmd.PersistentFlags().MarkHidden("alsologtostderr")
driverCmd.PersistentFlags().MarkHidden("log_backtrace_at")
Expand Down
63 changes: 63 additions & 0 deletions cmd/directpv/ready.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// This file is part of MinIO DirectPV
// Copyright (c) 2022 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
"context"
"fmt"
"net"
"net/http"

"k8s.io/klog/v2"
)

const (
readinessPath = "/ready"
)

func serveReadinessEndpoint(ctx context.Context) error {
// define http server and server handler
server := &http.Server{}
mux := http.NewServeMux()
mux.HandleFunc(readinessPath, readinessHandler)
server.Handler = mux

lc := net.ListenConfig{}
listener, lErr := lc.Listen(ctx, "tcp", fmt.Sprintf(":%v", readinessPort))
if lErr != nil {
return lErr
}

go func() {
klog.V(3).Infof("Starting to serve readiness endpoint in port: %d", readinessPort)
if err := server.Serve(listener); err != nil {
klog.Errorf("Failed to serve readiness endpoint: %v", err)
}
}()

return nil
}

// readinessHandler - Checks if the process is up. Always returns success.
func readinessHandler(w http.ResponseWriter, r *http.Request) {
klog.V(5).Infof("readiness request: %v", r)
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.WriteHeader(http.StatusOK)
}
80 changes: 8 additions & 72 deletions cmd/directpv/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ package main

import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"net/http"
"os"
"time"

"github.com/google/uuid"
ctrl "github.com/minio/directpv/pkg/controller"
"github.com/minio/directpv/pkg/converter"
"github.com/minio/directpv/pkg/drive"
"github.com/minio/directpv/pkg/fs/xfs"
id "github.com/minio/directpv/pkg/identity"
Expand All @@ -39,66 +34,10 @@ import (
losetup "gopkg.in/freddierice/go-losetup.v1"

"github.com/container-storage-interface/spec/lib/go/csi"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
)

const (
conversionCAFile = "/etc/conversion/CAs/ca.pem"
)

var (
errInvalidConversionHealthzURL = errors.New("the `--conversion-webhook-healthz-url` flag is unset/empty")
errMountFailure = errors.New("could not mount the drive")
)

func waitForConversionWebhook(ctx context.Context) error {
if conversionHealthzURL == "" {
return errInvalidConversionHealthzURL
}

caCert, err := os.ReadFile(conversionCAFile)
if err != nil {
klog.V(2).Infof("Error while reading cacert %v", err)
return err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)

client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
},
},
Timeout: 2 * time.Second,
}
defer client.CloseIdleConnections()

if err := retry.OnError(
wait.Backoff{
Duration: 1 * time.Second,
Factor: 2.0,
Jitter: 0.1,
Steps: 5,
Cap: 1 * time.Minute,
},
func(err error) bool {
return err != nil
},
func() error {
_, err := client.Get(conversionHealthzURL)
if err != nil {
klog.V(2).Infof("Waiting for conversion webhook: %v", err)
}
return err
}); err != nil {
return err
}

return nil
}
var errMountFailure = errors.New("could not mount the drive")

func checkXFS(ctx context.Context, reflinkSupport bool) error {
mountPoint, err := os.MkdirTemp("", "xfs.check.mnt.")
Expand Down Expand Up @@ -159,16 +98,6 @@ func run(ctxMain context.Context, args []string) error {
)
}

// Start conversion webserver
if err := converter.ServeConversionWebhook(ctx); err != nil {
return err
}

if err := waitForConversionWebhook(ctx); err != nil {
return err
}
klog.V(3).Info("The conversion webhook is live!")

idServer, err := id.NewIdentityServer(identity, Version, map[string]string{})
if err != nil {
return err
Expand Down Expand Up @@ -240,6 +169,13 @@ func run(ctxMain context.Context, args []string) error {
}
}()

go func() {
if err := serveReadinessEndpoint(ctx); err != nil {
klog.ErrorS(err, "failed to serve readiness endpoint")
errChan <- err
}
}()

err = <-errChan
if err != nil {
cancel()
Expand Down
17 changes: 0 additions & 17 deletions pkg/converter/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"

directcsi "github.com/minio/directpv/pkg/apis/direct.csi.min.io/v1beta4"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -35,22 +34,6 @@ type migrateFunc func(object *unstructured.Unstructured, toVersion string) error

var errUnsupportedCRDKind = errors.New("unsupported CRD Kind")

func convertDriveCRD(Object *unstructured.Unstructured, toVersion string) (*unstructured.Unstructured, metav1.Status) {
convertedObject := Object.DeepCopy()
if err := migrate(convertedObject, toVersion); err != nil {
return nil, statusErrorWithMessage(err.Error())
}
return convertedObject, statusSucceed()
}

func convertVolumeCRD(Object *unstructured.Unstructured, toVersion string) (*unstructured.Unstructured, metav1.Status) {
convertedObject := Object.DeepCopy()
if err := migrate(convertedObject, toVersion); err != nil {
return nil, statusErrorWithMessage(err.Error())
}
return convertedObject, statusSucceed()
}

// MigrateList migrate the list to the provided group version
func MigrateList(fromList, toList *unstructured.UnstructuredList, groupVersion schema.GroupVersion) error {
fromList.DeepCopyInto(toList)
Expand Down
Loading

0 comments on commit 8da64cc

Please sign in to comment.