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

Delete and redeploy object upon error 'field is immutable' #940

Merged
merged 10 commits into from
Oct 10, 2018
24 changes: 22 additions & 2 deletions pkg/skaffold/deploy/kubectl/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ limitations under the License.
package kubectl

import (
"bufio"
"bytes"
"context"
"io"
"os/exec"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
Expand Down Expand Up @@ -56,8 +59,25 @@ func (c *CLI) Apply(ctx context.Context, out io.Writer, manifests ManifestList)
return nil, nil
}

if err := c.Run(ctx, manifests.Reader(), out, "apply", c.Flags.Apply, "-f", "-"); err != nil {
return nil, errors.Wrap(err, "kubectl apply")
buf := bytes.NewBuffer([]byte{})
writer := bufio.NewWriter(buf)
if err := c.Run(ctx, manifests.Reader(), writer, "apply", c.Flags.Apply, "-f", "-"); err != nil {
if !strings.Contains(buf.String(), "field is immutable") {
return nil, err
}
// If the output contains the string 'field is immutable', we want to delete the object and recreate it
// See Issue #891 for more information
if err := c.Detete(ctx, out, manifests); 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.

this will need a rebase on balint's PR

return nil, errors.Wrap(err, "deleting manifest")
}
if err := c.Run(ctx, manifests.Reader(), out, "apply", c.Flags.Apply, "-f", "-"); err != nil {
return nil, errors.Wrap(err, "kubectl apply after deletion")
}
} else {
// Write output to out
if _, err := out.Write(buf.Bytes()); err != nil {
return nil, errors.Wrap(err, "writing to out")
}
}

return updated, nil
Expand Down