Skip to content

Commit

Permalink
karmada-webhook: fix the no such host error
Browse files Browse the repository at this point in the history
Signed-off-by: zhzhuang-zju <[email protected]>
  • Loading branch information
zhzhuang-zju committed Jan 23, 2025
1 parent 07747ed commit 6e508a6
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ spec:
strategy: Webhook
webhook:
clientConfig:
url: https://karmada-webhook.karmada-system.svc:443/convert
url: "https://{{name}}.{{namespace}}.svc:443/convert"
caBundle: "{{caBundle}}"
conversionReviewVersions: ["v1"]
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ spec:
strategy: Webhook
webhook:
clientConfig:
url: https://karmada-webhook.karmada-system.svc:443/convert
url: "https://{{name}}.{{namespace}}.svc:443/convert"
caBundle: "{{caBundle}}"
conversionReviewVersions: ["v1"]
22 changes: 18 additions & 4 deletions operator/pkg/tasks/init/karmadaresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func runCrds(r workflow.RunData) error {
}

caBase64 := base64.StdEncoding.EncodeToString(cert.CertData())
if err := patchCrds(crdsClient, crdsPatchPath, caBase64); err != nil {
if err := patchCrds(data, crdsClient, crdsPatchPath, caBase64); err != nil {
return fmt.Errorf("failed to patch karmada crds, err: %w", err)
}

Expand All @@ -149,14 +149,28 @@ func createCrds(crdsClient *crdsclient.Clientset, crdsPath string) error {
return nil
}

func patchCrds(crdsClient *crdsclient.Clientset, patchPath string, caBundle string) error {
func patchCrds(data InitData, crdsClient *crdsclient.Clientset, patchPath string, caBundle string) error {
for _, file := range util.ListFileWithSuffix(patchPath, ".yaml") {
reg, err := regexp.Compile("{{caBundle}}")
caBundleReg, err := regexp.Compile("{{caBundle}}")
if err != nil {
return err
}

crdBytes, err := util.ReplaceYamlForReg(file.AbsPath, caBundle, reg)
nameReg, err := regexp.Compile("{{name}}")
if err != nil {
return err
}

namespaceReg, err := regexp.Compile("{{namespace}}")
if err != nil {
return err
}

crdBytes, err := util.ReplaceYamlForRegs(file.AbsPath, map[*regexp.Regexp]string{
caBundleReg: caBundle,
nameReg: util.KarmadaWebhookName(data.GetName()),
namespaceReg: data.GetNamespace(),
})
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions operator/pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ func ReplaceYamlForReg(path, destResource string, reg *regexp.Regexp) ([]byte, e
return yaml.YAMLToJSON([]byte(repl))
}

// ReplaceYamlForRegs replace content of yaml file with Regexps
func ReplaceYamlForRegs(path string, replacements map[*regexp.Regexp]string) ([]byte, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}

repl := string(data)
for reg, dest := range replacements {
repl = reg.ReplaceAllString(repl, dest)
}

return yaml.YAMLToJSON([]byte(repl))
}

// ContainAllTasks checks if all tasks in the subset are present in the tasks slice.
// Returns an error if any subset task is not found; nil otherwise.
func ContainAllTasks(tasks, subset []workflow.Task) error {
Expand Down
20 changes: 16 additions & 4 deletions pkg/karmadactl/cmdinit/karmada/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func InitKarmadaResources(dir, caBase64, systemNamespace string) error {
if path.Ext(v) != ".yaml" {
continue
}
if err := patchCRDs(crdClient, caBase64, v); err != nil {
if err := patchCRDs(crdClient, caBase64, systemNamespace, v); err != nil {
return err
}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func createExtraResources(clientSet *kubernetes.Clientset, dir string) error {
return nil
}

func crdPatchesResources(filename, caBundle string) ([]byte, error) {
func crdPatchesResources(filename, caBundle, systemNamespace string) ([]byte, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
Expand All @@ -185,6 +185,18 @@ func crdPatchesResources(filename, caBundle string) ([]byte, error) {
}
repl := re.ReplaceAllString(string(data), caBundle)

re, err = regexp.Compile("{{name}}")
if err != nil {
return nil, err
}
repl = re.ReplaceAllString(repl, names.KarmadaWebhookComponentName)

re, err = regexp.Compile("{{namespace}}")
if err != nil {
return nil, err
}
repl = re.ReplaceAllString(repl, systemNamespace)

return []byte(repl), nil
}

Expand Down Expand Up @@ -220,8 +232,8 @@ func createCRDs(crdClient clientset.Interface, filename string) error {
}

// patchCRDs patch crd resource
func patchCRDs(crdClient clientset.Interface, caBundle, filename string) error {
data, err := crdPatchesResources(filename, caBundle)
func patchCRDs(crdClient clientset.Interface, caBundle, systemNamespace, filename string) error {
data, err := crdPatchesResources(filename, caBundle, systemNamespace)
if err != nil {
return err
}
Expand Down
68 changes: 68 additions & 0 deletions pkg/karmadactl/cmdinit/karmada/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2024 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package karmada

import (
"os"
"testing"

"github.com/karmada-io/karmada/pkg/util/names"
)

func TestCrdPatchesResources(t *testing.T) {
tests := []struct {
name string
content string
caBundle string
systemNs string
expectedContent string
}{
{
name: "simple replacement",
content: "caBundle: {{caBundle}}\nname: {{name}}\nnamespace: {{namespace}}",
caBundle: "testCaBundle",
systemNs: "testNamespace",
expectedContent: "caBundle: testCaBundle\nname: " + names.KarmadaWebhookComponentName + "\nnamespace: testNamespace",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpfile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(tmpfile.Name()) // 清理临时文件

if _, err := tmpfile.Write([]byte(tt.content)); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("failed to close temp file: %v", err)
}

result, err := crdPatchesResources(tmpfile.Name(), tt.caBundle, tt.systemNs)
if err != nil {
t.Errorf("crdPatchesResources() error = %v", err)
}

if string(result) != tt.expectedContent {
t.Errorf("crdPatchesResources() = %v, want %v", string(result), tt.expectedContent)
}
})
}
}

0 comments on commit 6e508a6

Please sign in to comment.