Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replacing hard coded cluster.local with variable #2892

Merged
merged 9 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pkg/reconciler/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ limitations under the License.

package reconciler

import "fmt"
import (
"fmt"
"github.com/knative/serving/pkg/utils"
)

func GetK8sServiceFullname(name string, namespace string) string {
return fmt.Sprintf("%s.%s.svc.cluster.local", name, namespace)
clusterDomainName, err := utils.GetClusterDomainName()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we compute this once when the binary starts up so that we won't read a file everytime we want to get the full name of a K8s Service?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this operation is infrequent so the should not be any quantifiable performance impact. Also this approach allows different reaction of a failure of getting domain name. In some cases it might be useful. Appreciate other folks to chime in and share their opinion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's going to be constant, I don't see reasons for it to be recomputed each time.
If we need to test failure cases, then go provides various methods to spoof failures (substitute files, use different file path, mockout internal function).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, does it work for you, if I call it from main, set global var in utils package and then when domain name is needed utils.ClusterDomainName var can be used?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather, I'd recommend to use this: https://golang.org/pkg/sync/#Once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see new solution based on once.Do()

if err != nil {
clusterDomainName = "cluster.local"
}
return fmt.Sprintf("%s.%s.svc.%s", name, namespace, clusterDomainName)
}

func GetServingK8SServiceNameForObj(name string) string {
Expand Down
57 changes: 57 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2018 The Knative 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 utils

import (
"bufio"
"fmt"
"io"
"os"
"strings"
)

const (
resolverFileName = "/etc/resolv.conf"
)

// GetClusterDomainName returns cluster's domain name or an error
// Closes issue: https://github.com/knative/eventing/issues/714
func GetClusterDomainName() (string, error) {
f, err := os.Open(resolverFileName)
if err != nil {
return "", err
}
defer f.Close()

return getClusterDomainName(f)
}

func getClusterDomainName(r io.Reader) (string, error) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
elements := strings.Split(scanner.Text(), " ")
if elements[0] != "search" {
continue
}
for i := 1; i < len(elements)-1; i++ {
if strings.HasPrefix(elements[i], "svc.") {
return elements[i][4:], nil
}
}
}
return "", fmt.Errorf("%s does not seem to be a valid kubernetes resolv.conf file", resolverFileName)
}
80 changes: 80 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2018 The Knative 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 utils

import (
"strings"
"testing"
)

func TestGetDomainName(t *testing.T) {
tests := []struct {
descr string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/descr/name/?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

resolvConf string
domainName string
shouldFail bool
}{
{
descr: "all good",
resolvConf: `
nameserver 1.1.1.1
search default.svc.abc.com svc.abc.com abc.com
options ndots:5
`,
domainName: "abc.com",
shouldFail: false,
},
{
descr: "missing search line",
resolvConf: `
nameserver 1.1.1.1
options ndots:5
`,
domainName: "",
shouldFail: true,
},
{
descr: "non k8s resolv.conf format",
resolvConf: `
nameserver 1.1.1.1
search abc.com xyz.com
options ndots:5
`,
domainName: "",
shouldFail: true,
},
}
for _, tt := range tests {
dn, err := getClusterDomainName(strings.NewReader(tt.resolvConf))
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if got, want := (err != nil), t.shouldFail; got != want {
   t.Errorf("....")
} else if err == nil {
  if got, want := domain, t.domainName; got != want {
    t.Errorf("... domain: got: %s, want: %s",...)
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this sequence, it does not catch directly func returns err == nil but the expectation was to fail. Do you think it is sufficient to catch this condition just on domain name mismatch? Currently err==nil validation covers 2 scenarios, 1 incorrect parsing of domain name when func returns nil and wrong domain name and 2 not detecting corruption of resolv.conf file. Please let me know if this explanation makes sense to you. Appreciate a lot your feedback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does not catch directly func returns err == nil but the expectation was to fail -- wouldn't (err != nil) != t.shouldFail condition catch that?
We want to make sure errors are returned when it's an error condition. But since your tests don't discriminate errors, just making sure error was returned in the erroneous condition will suffice. That's what my suggested change does.
Hope, this makes sense :)

if !tt.shouldFail {
t.Errorf("Test %s failed with error: %q but is not supposed to.", tt.descr, err)
} else {
continue
}
}
if err == nil {
if tt.shouldFail {
t.Errorf("Test %s succeeded but supposed to fail.", tt.descr)
continue
}
if dn != tt.domainName {
t.Errorf("Test %s failed expected %s but got %s", tt.descr, tt.domainName, dn)
}
}
}
}