Skip to content

Commit

Permalink
support http(s) proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Ryotaro Banno <ryotaro.banno@gmail.com>
  • Loading branch information
ushitora-anqou committed Dec 6, 2024
1 parent fc095a2 commit 23153ed
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
9 changes: 9 additions & 0 deletions charts/mantle/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ spec:
{{- with .Values.controller.gcInterval }}
- --gc-interval={{ . }}
{{- end }}
{{- with .Values.controller.httpProxy }}
- --http-proxy={{ . }}
{{- end }}
{{- with .Values.controller.httpsProxy }}
- --https-proxy={{ . }}
{{- end }}
{{- with .Values.controller.noProxy }}
- --no-proxy={{ . }}
{{- end }}
env:
- name: POD_NAME
valueFrom:
Expand Down
30 changes: 30 additions & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ var (
caCertConfigMapSrc string
caCertKeySrc string
gcInterval string
httpProxy string
httpsProxy string
noProxy string

scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
Expand Down Expand Up @@ -98,6 +101,12 @@ func init() {
"The default value is ca.crt. This option is just ignored if --ca-cert-configmap isn't specified.")
flags.StringVar(&gcInterval, "gc-interval", "1h",
"The time period between each garbage collection for orphaned resources.")
flags.StringVar(&httpProxy, "http-proxy", "",
"The proxy URL for HTTP requests to the object storage and the gRPC endpoint of secondary mantle.")
flags.StringVar(&httpProxy, "https-proxy", "",
"The proxy URL for HTTPS requests to the object storage and the gRPC endpoint of secondary mantle.")
flags.StringVar(&httpProxy, "no-proxy", "",
"A string that contains comma-separated values specifying hosts that should be excluded from proxying.")

goflags := flag.NewFlagSet("goflags", flag.ExitOnError)
zapOpts.Development = true
Expand Down Expand Up @@ -179,6 +188,11 @@ func setupReconcilers(mgr manager.Manager, primarySettings *controller.PrimarySe
CACertConfigMap: caCertConfigMap,
CACertKey: &caCertKeySrc,
},
&controller.ProxySettings{
HttpProxy: httpProxy,
HttpsProxy: httpsProxy,
NoProxy: noProxy,
},
)
if err := backupReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MantleBackup")
Expand Down Expand Up @@ -233,6 +247,22 @@ func setupStandalone(mgr manager.Manager) error {
}

func setupPrimary(ctx context.Context, mgr manager.Manager, wg *sync.WaitGroup) error {
// Setup environment variables related to proxies before creating a gRPC client.
// cf. https://github.com/grpc/grpc-go/blob/adad26df1826bf2fb66ad56ff32a62b98bf5cb3a/Documentation/proxy.md
// cf. https://pkg.go.dev/golang.org/x/net/http/httpproxy
if err := os.Setenv("HTTP_PROXY", httpProxy); err != nil {
setupLog.Error(err, "failed to set HTTP_PROXY environment variable")
return err
}
if err := os.Setenv("HTTPS_PROXY", httpsProxy); err != nil {
setupLog.Error(err, "failed to set HTTPS_PROXY environment variable")
return err
}
if err := os.Setenv("NO_PROXY", noProxy); err != nil {
setupLog.Error(err, "failed to set NO_PROXY environment variable")
return err
}

conn, err := grpc.NewClient(
mantleServiceEndpoint,
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down
21 changes: 21 additions & 0 deletions internal/controller/mantlebackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ type ObjectStorageSettings struct {
Endpoint string
}

type ProxySettings struct {
HttpProxy string
HttpsProxy string
NoProxy string
}

// MantleBackupReconciler reconciles a MantleBackup object
type MantleBackupReconciler struct {
client.Client
Expand All @@ -85,6 +91,7 @@ type MantleBackupReconciler struct {
envSecret string
objectStorageSettings *ObjectStorageSettings // This should be non-nil if and only if role equals 'primary' or 'secondary'.
objectStorageClient objectstorage.Bucket
proxySettings *ProxySettings
}

// NewMantleBackupReconciler returns NodeReconciler.
Expand All @@ -97,6 +104,7 @@ func NewMantleBackupReconciler(
podImage string,
envSecret string,
objectStorageSettings *ObjectStorageSettings,
proxySettings *ProxySettings,
) *MantleBackupReconciler {
return &MantleBackupReconciler{
Client: client,
Expand All @@ -109,6 +117,7 @@ func NewMantleBackupReconciler(
podImage: podImage,
envSecret: envSecret,
objectStorageSettings: objectStorageSettings,
proxySettings: proxySettings,
}
}

Expand Down Expand Up @@ -1325,6 +1334,18 @@ func (r *MantleBackupReconciler) createOrUpdateExportDataUploadJob(ctx context.C
},
},
},
{
Name: "HTTP_PROXY",
Value: r.proxySettings.HttpProxy,
},
{
Name: "HTTPS_PROXY",
Value: r.proxySettings.HttpProxy,
},
{
Name: "NO_PROXY",
Value: r.proxySettings.NoProxy,
},
},
Image: r.podImage,
ImagePullPolicy: corev1.PullIfNotPresent,
Expand Down
15 changes: 14 additions & 1 deletion internal/controller/mantlebackup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ var _ = Describe("MantleBackup controller", func() {
"dummy image",
"",
nil,
nil,
)
reconciler.ceph = testutil.NewFakeRBD()
err := reconciler.SetupWithManager(mgrUtil.GetManager())
Expand Down Expand Up @@ -355,6 +356,11 @@ var _ = Describe("MantleBackup controller", func() {
CACertConfigMap: nil,
CACertKey: nil,
},
&ProxySettings{
HttpProxy: "",
HttpsProxy: "",
NoProxy: "",
},
)
reconciler.ceph = testutil.NewFakeRBD()

Expand Down Expand Up @@ -800,7 +806,7 @@ var _ = Describe("prepareForDataSynchronization", func() {
}

mbr := NewMantleBackupReconciler(ctrlClient,
ctrlClient.Scheme(), "test", RolePrimary, nil, "dummy image", "", nil)
ctrlClient.Scheme(), "test", RolePrimary, nil, "dummy image", "", nil, nil)

ret, err := mbr.prepareForDataSynchronization(context.Background(),
backup, grpcClient)
Expand Down Expand Up @@ -1232,6 +1238,11 @@ var _ = Describe("export", func() {
"dummy image",
"",
nil,
&ProxySettings{
HttpProxy: "",
HttpsProxy: "",
NoProxy: "",
},
)

ns = resMgr.CreateNamespace()
Expand Down Expand Up @@ -1351,6 +1362,7 @@ var _ = Describe("export", func() {
"dummy image",
"",
nil,
nil,
)
ns2 := resMgr.CreateNamespace()
createAndExportMantleBackup(mbr2, "target2", ns2)
Expand Down Expand Up @@ -1385,6 +1397,7 @@ var _ = Describe("import", func() {
"dummy-image",
"dummy-env-secret",
&ObjectStorageSettings{},
nil,
)
mbr.objectStorageClient = mockObjectStorage
mbr.ceph = testutil.NewFakeRBD()
Expand Down
1 change: 1 addition & 0 deletions internal/controller/mantlerestore_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (test *mantleRestoreControllerUnitTest) setupEnv() {
"dummy image",
"",
nil,
nil,
)
backupReconciler.ceph = testutil.NewFakeRBD()
err := backupReconciler.SetupWithManager(test.mgrUtil.GetManager())
Expand Down

0 comments on commit 23153ed

Please sign in to comment.