-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
kubeadm: config migrate handles more valid configs #71315
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ limitations under the License. | |
package config | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net" | ||
"reflect" | ||
|
@@ -35,28 +36,6 @@ import ( | |
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util" | ||
) | ||
|
||
// AnyConfigFileAndDefaultsToInternal reads either a InitConfiguration or JoinConfiguration and unmarshals it | ||
func AnyConfigFileAndDefaultsToInternal(cfgPath string) (runtime.Object, error) { | ||
b, err := ioutil.ReadFile(cfgPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
gvks, err := kubeadmutil.GroupVersionKindsFromBytes(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// First, check if the gvk list has InitConfiguration and in that case try to unmarshal it | ||
if kubeadmutil.GroupVersionKindsHasInitConfiguration(gvks...) { | ||
return ConfigFileAndDefaultsToInternalConfig(cfgPath, &kubeadmapiv1beta1.InitConfiguration{}) | ||
} | ||
if kubeadmutil.GroupVersionKindsHasJoinConfiguration(gvks...) { | ||
return JoinConfigFileAndDefaultsToInternalConfig(cfgPath, &kubeadmapiv1beta1.JoinConfiguration{}) | ||
} | ||
return nil, errors.Errorf("didn't recognize types with GroupVersionKind: %v", gvks) | ||
} | ||
|
||
// MarshalKubeadmConfigObject marshals an Object registered in the kubeadm scheme. If the object is a InitConfiguration or ClusterConfiguration, some extra logic is run | ||
func MarshalKubeadmConfigObject(obj runtime.Object) ([]byte, error) { | ||
switch internalcfg := obj.(type) { | ||
|
@@ -181,3 +160,46 @@ func ChooseAPIServerBindAddress(bindAddress net.IP) (net.IP, error) { | |
} | ||
return ip, nil | ||
} | ||
|
||
// MigrateOldConfigFromFile migrates an old configuration from a file into a new one (returned as a byte slice). Only kubeadm kinds are migrated. Others are silently ignored. | ||
func MigrateOldConfigFromFile(cfgPath string) ([]byte, error) { | ||
newConfig := [][]byte{} | ||
|
||
cfgBytes, err := ioutil.ReadFile(cfgPath) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
gvks, err := kubeadmutil.GroupVersionKindsFromBytes(cfgBytes) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
// Migrate InitConfiguration and ClusterConfiguration if there are any in the config | ||
if kubeadmutil.GroupVersionKindsHasInitConfiguration(gvks...) || kubeadmutil.GroupVersionKindsHasClusterConfiguration(gvks...) { | ||
o, err := ConfigFileAndDefaultsToInternalConfig(cfgPath, &kubeadmapiv1beta1.InitConfiguration{}) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
b, err := MarshalKubeadmConfigObject(o) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
newConfig = append(newConfig, b) | ||
} | ||
|
||
// Migrate JoinConfiguration if there is any | ||
if kubeadmutil.GroupVersionKindsHasJoinConfiguration(gvks...) { | ||
o, err := JoinConfigFileAndDefaultsToInternalConfig(cfgPath, &kubeadmapiv1beta1.JoinConfiguration{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above. |
||
if err != nil { | ||
return []byte{}, err | ||
} | ||
b, err := MarshalKubeadmConfigObject(o) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
newConfig = append(newConfig, b) | ||
} | ||
|
||
return bytes.Join(newConfig, []byte(constants.YAMLDocumentSeparator)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can do some refactoring for
ConfigFileAndDefaultsToInternalConfig
, since the file will be read twice. There is no need.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's on my agenda for the next release cycle. Decoupling InitConfiguration and ClusterConfiguration is one of my priorities. If we refactor the APIs and make ClusterConfiguration more standalone and introduce an internal type that houses the usual pair (ClusterConfiguration + NodeRegistrationOptions for the current node) we can eliminate most of the confusion and optimize things at all levels.
But for now at the end of this release cycle it's best to keep the changes as small as possible and just fix the bug at hand.